473,467 Members | 1,410 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Variable swapping question

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. What would happen in the
absence of temp?
Chad
Jul 5 '08 #1
3 1100
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"
Jul 5 '08 #2
On Jul 4, 6:44*pm, Keith Thompson <ks...@mib.orgwrote:
Chad <cdal...@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/>?

--
I was just curious why you needed temp. I did the following

m-net% more swap.c
#include <stdio.h>
void swap(int *x, int *y)
{
int temp;
/*temp = *x ; */
*x = *y;
/* *y = temp; */

}

int main(void)
{
int a = 10;
int b = 20;

swap(&a, &b);

printf("After swap: a=%d , b=%d\n", a, b);

return 0;
}
m-net% ./swap
After swap: a=20 , b=20
m-net%

And now just thought about what you said. Now I need about a hour for
it to really sink in.

Jul 5 '08 #3
On Fri, 4 Jul 2008 19:00:47 -0700 (PDT), Chad posted:
On Jul 4, 6:44*pm, Keith Thompson <ks...@mib.orgwrote:
>Chad <cdal...@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/>?

--

I was just curious why you needed temp. I did the following

m-net% more swap.c
#include <stdio.h>
void swap(int *x, int *y)
{
int temp;
/*temp = *x ; */
*x = *y;
/* *y = temp; */

}

int main(void)
{
int a = 10;
int b = 20;

swap(&a, &b);

printf("After swap: a=%d , b=%d\n", a, b);

return 0;
}
m-net% ./swap
After swap: a=20 , b=20
m-net%

And now just thought about what you said. Now I need about a hour for
it to really sink in.
I see no mystery in this result; you'll find that this type of swap needs a
temp. You can swap in other ways, say, using a macro, but the pointer
version is simply better all around.
--
The difference between a moral man and a man of honor is that the latter
regrets a discreditable act, even when it has worked and he has not been
caught.
H. L. Mencken
Jul 5 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Christopher Jeris | last post by:
Please help me understand the differences, in semantics, browser support and moral preferredness, between the following three methods of swapping content in and out of a page via JavaScript. I...
50
by: Steve | last post by:
How do you rewrite the swap function without using a tmp variable in the swap function???? int main() { int x = 3; int y = 5; // Passing by reference
28
by: rajendra.stalekar | last post by:
Hi Folks!!! I have a string let's say "hi" and got to reverse it using just a single variable for swapping, how do I do it? Regards, Rajendra S.
25
by: indrawati.yahya | last post by:
OK, before you all stab me with your OT pitchfork, I just want to mention that this question is indeed related to c.l.c++. My question is, does the well-known method to swap two integers produce...
11
by: Juha Nieminen | last post by:
Assume we have this: std::list<Typelist1(10, 1), list2(20, 2); std::list<Type>::iterator iter = list1.end(); list1.swap(list2); What happens here, according to the standard? 1) 'iter'...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.