By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,851 Members | 2,097 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,851 IT Pros & Developers. It's quick & easy.

swap of two no.?????

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

Nov 7 '06 #1
Share this Question
Share on Google+
10 Replies


P: n/a
Cla
im******@gmail.com wrote:
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.
Nov 7 '06 #2

P: n/a

im******@gmail.com wrote:
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?

Nov 7 '06 #3

P: n/a
In article <11**********************@m73g2000cwd.googlegroups .com>,
im******@gmail.com <im******@gmail.comwrote:
>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.

--
Prototypes are supertypes of their clones. -- maplesoft
Nov 7 '06 #4

P: n/a
im******@gmail.com:
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);

--

Frederick Gotham
Nov 7 '06 #5

P: n/a
im******@gmail.com wrote:
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 */
}
Nov 7 '06 #6

P: n/a

im******@gmail.com wrote:
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

Nov 8 '06 #7

P: n/a
"im******@gmail.com" <im******@gmail.comwrote:
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
Nov 9 '06 #8

P: n/a
rl*@hoekstra-uitgeverij.nl (Richard Bos) writes:
"im******@gmail.com" <im******@gmail.comwrote:
>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.
[...]

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.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 10 '06 #9

P: n/a
Keith Thompson <ks***@mib.orgwrote:
rl*@hoekstra-uitgeverij.nl (Richard Bos) writes:
"im******@gmail.com" <im******@gmail.comwrote:
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.
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
Nov 10 '06 #10

P: n/a
rl*@hoekstra-uitgeverij.nl (Richard Bos) writes:
Keith Thompson <ks***@mib.orgwrote:
[...]
>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.
Ah, I see your point.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 10 '06 #11

This discussion thread is closed

Replies have been disabled for this discussion.