pointers switch function

A

apropo

#include <stdio.h>
#include <stdlib.h>
void tausche(int *a,int *b)
{
int *c=a;
a=b;
b=c;
}
int main(void)
{
int *x,*y;
*x=4;
*y=5;
tausche(x,y);
printf("x:%d,y:%d",x,y);
getch();
return 0;
}

i want this function - tausche - to switch the values stored at their
address, but it doesn't. why and how would it be correct ?
 
J

J Baskin

apropo said:
#include <stdio.h>
#include <stdlib.h>
void tausche(int *a,int *b)
{
int *c=a;
a=b;
b=c;
}
int main(void)
{
int *x,*y;
*x=4;
*y=5;
tausche(x,y);
printf("x:%d,y:%d",x,y);
getch();
return 0;
}

i want this function - tausche - to switch the values stored at their
address, but it doesn't. why and how would it be correct ?

Your function *should* switch the addresses pointed to by each pointer. So,
*x should return 5 after the function is called. However, your main()
procedure contains no way to test whether or not this is happening.

I think you want to write:
printf("x:%d,y:%d",*x,*y);
 
J

J Baskin

oops, and there's another problem. When you call your function, it creates
two new variables, a and b. a contains the value of x (a memory address), b
contains the value of y (also a memory address). When you switch them, a
contains the value of y and b contains the value of x. But then, when the
function quits, a and b are destroyed.

Here's an example of a replacement:

void tausche(int *a,int *b)
{
int c;
c=*a;
*a=*b;
*b=c;
}

If you do want to switch the actual addresses, you need to pass double
pointers.
 
A

apropo

Your function *should* switch the addresses pointed to by each pointer. So,
*x should return 5 after the function is called. However, your main()
procedure contains no way to test whether or not this is happening.

I think you want to write:
printf("x:%d,y:%d",*x,*y);

you are right, but the code is still not as suposed to be, the _same_
output (?)
 
J

Joe Wright

apropo said:
#include <stdio.h>
#include <stdlib.h>
void tausche(int *a,int *b)
{
int *c=a;
a=b;
b=c;
}
int main(void)
{
int *x,*y;
*x=4;
*y=5;
tausche(x,y);
printf("x:%d,y:%d",x,y);
getch();
return 0;
}

i want this function - tausche - to switch the values stored at their
address, but it doesn't. why and how would it be correct ?

What are you two smoking? :)

#include <stdio.h>
void tauche(int *a, int *b) {
int c = *a;
*a = *b;
*b = c;
}
int main(void) {
int x = 4, y = 5;
printf("x:%d, y:%d\n", x, y);
tauche(&x, &y);
printf("x:%d, y:%d\n", x, y);
return 0;
}
 
M

Martin Ambuhl

apropo said:
#include <stdio.h>
#include <stdlib.h>
void tausche(int *a,int *b)
{
int *c=a;
a=b;
b=c;
}
int main(void)
{
int *x,*y;
*x=4;
*y=5;
tausche(x,y);
printf("x:%d,y:%d",x,y);
getch();
return 0;
}

i want this function - tausche - to switch the values stored at their
address, but it doesn't. why and how would it be correct ?

Avoiding all the problems in your code with x and y not pointing to
anywhere useful and getch() not being a standard function:

#include <stdio.h>
#include <stdlib.h>

void tausche(int *a, int *b)
{
int c = *a;
*a = *b;
*b = c;
}
int main(void)
{
int x = 4, y = 5;
tausche(&x, &y);
printf("x:%d,y:%d\n", x, y);
getchar();
return 0;
}
 
M

Martin Ambuhl

J said:
apropo wrote:




Your function *should* switch the addresses pointed to by each pointer. So,
*x should return 5 after the function is called.

x and y point into outer space somewhere. *x and *y are meaningless when x
and y have themselves no useful meaning.
However, your main()
procedure contains no way to test whether or not this is happening.

I think you want to write:
printf("x:%d,y:%d",*x,*y);

That just adds another level of dereferencing uninitialized pointers.
 
T

Thes

apropo said:
#include <stdio.h>
#include <stdlib.h>
void tausche(int *a,int *b)
{
int *c=a;
a=b;
b=c;
}
int main(void)
{
int *x,*y;
*x=4;
*y=5;
tausche(x,y);
printf("x:%d,y:%d",x,y);
getch();
return 0;
}

i want this function - tausche - to switch the values stored at their
address, but it doesn't. why and how would it be correct ?

You're passing in pointers to your data, and then switch the POINTERS
inside the function. This is then lost when you exit the function. Once
you return to main(), the switching is irrelevant as you haven't
switched the DATA.

Perhaps:

void tausche(int* A, int* b)
{
int c = *a;
*a = *b;
*b = c;
}
 

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

No members online now.

Forum statistics

Threads
474,140
Messages
2,570,810
Members
47,357
Latest member
sitele8746

Latest Threads

Top