Chad <cd*****@gmail.comwrites:
Given something like the following
void swap(int *x, int *y)
{
int temp;
temp = *x;
*x = *y;
*y = temp;
}
How does having a temp variable allow for anything to safely swap with
itself? Like for example x = 10 and y = 10.
I assume you mean *x == 10 and *y == 10.
But what I suspect you *really* mean is x == y, i.e., x and y both
point to the same object. The swap function above works just fine in
that case; the value is saved to temp, then copied over itself, then
restored from temp. Is there some potential problem you had in mind?
What would happen in the
absence of temp?
Well, you couldn't do the first and third assignments, so it wouldn't
be able to swap anything. Or did you have something else in mind
instead of temp? Perhaps something like question 3.3b of the
comp.lang.c FAQ, <http://c-faq.com/>?
--
Keith Thompson (The_Other_Keith)
ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"