swap of two no.?????

I

imnilima

hey
i want to know that how to swap 2 values ,that is one is integer &
other is float using pointers in C.
 
C

Cla

hey
i want to know that how to swap 2 values ,that is one is integer &
other is float using pointers in C.

Instead, I always wanted to know more about history,
but I just cannot remember the dates.
Oh, well.
 
L

Lew Pitcher

hey
i want to know that how to swap 2 values ,that is one is integer &
other is float using pointers in C.

What you want is so wrong that it can't be answered.

Perhaps you aren't explaining your need correctly. Could you please
expand on what it is that you want?
 
W

Walter Roberson

i want to know that how to swap 2 values ,that is one is integer &
other is float using pointers in C.

Could you clarify what you mean? If your pointers are I and F to
start, then afterwards you want I to point to the float and F to
point to the int -- a pointer operation? Or you want the memory location
that used to hold the int to now hold the float and vice versa?
If so, with or without I and F being updated to point to the new locations?

If you are trying to change the pointers without moving the memory,
then you have the problem that the pointers would point to the wrong
types afterwards -- you cannot use an int pointer to point to a float.

If you are trying ot change the memory locations, then you have
the problem that int and float may be different sizes, so when you
move one of them there might not be enough room at the destination.

It is not uncommon for floats to have to be aligned on addresses that
are internally multiples of 4 bytes, but for int to only be restricted
to multiple of 2 bytes, so you can run into alignment problems even if
everything else worked out.
 
F

Frederick Gotham

(e-mail address removed):
i want to know that how to swap 2 values ,that is one is integer &
other is float using pointers in C.


void SwapIntWithFloat(int *const pi,float *const pf)
{
int const temp i = *pi;
*pi = *pf;
*pf = i;
}


You might want some safe-guard such as:

assert(pi); assert(pf);

assert(*pf>=INT_MIN && *pf<=INT_MAX);
 
M

Martin Ambuhl

hey
i want to know that how to swap 2 values ,that is one is integer &
other is float using pointers in C.


void perverse_swap(int *i, float *f)
{
int t;
t = *f; /* if you want f rounded instead of truncated, you know
what to do */
*f = *i;
*i = t;
/* error detection & handling left as an exercise */
}
 
M

mark_bluemel

hey
i want to know that how to swap 2 values ,that is one is integer &
other is float using pointers in C.

My mom told me that

a) "Hey" is not a polite way to greet people, especially when you don't
know them well
b) "I want" never gets
c) Capital letters are free
 
R

Richard Bos

hey
Ho-nonny-nonny.

i want to know that how to swap 2 values ,that is one is integer &
other is float using pointers in C.

Don't listen to the others. Your teacher obviously doesn't want you to
swap values, because that can easily be done with a single temp object,
no need for pointers at all. What he wants is that you swap the _bytes_
representing these values. Like this:

assert(sizeof (int) == sizeof (float));
{
unsigned char temp[sizeof (int)];
memcpy(temp, the_int, sizeof (int));
memcpy(the_int, the_float, sizeof (int));
memcpy(the_float, temp, sizeof (int));
}

Make sure you use the float as soon as possible, so that you'll be told
if the int didn't contain a valid float representation.

Richard
 
K

Keith Thompson

Don't listen to the others. Your teacher obviously doesn't want you to
swap values, because that can easily be done with a single temp object,
no need for pointers at all. What he wants is that you swap the _bytes_
representing these values.
[...]

It's not at all obvious *what* this teacher wants him to do. Neither
swapping the values nor swapping the representations makes much sense,
though both are possible (assuming no range errors if we're swapping
values, and the same size if we're swapping representations).

I think the only appropriate response is "Huh?". Or rather, we need
to know *exactly* what's required.
 
R

Richard Bos

It's not at all obvious *what* this teacher wants him to do. Neither
swapping the values nor swapping the representations makes much sense,

Nevertheless, I think it would be better for him in the end if he handed
in my solution, as-is.

Richard
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,432
Messages
2,571,681
Members
48,796
Latest member
Greg L.

Latest Threads

Top