Solved

# Pointer question

Posted on 2006-05-16
Medium Priority
245 Views
I am learning C and I feel a bit confused by pointer. i write this simple code to test:

My idea is that, to swap two values, I can either

1) swapping two values stored in vars
2) swapping values' pointers

For example, I have two vars, a and b. Pointers to a and b are ffda and ffdc (in my local machine).

Innitially, a with adr ffda has the value of 5; b with addr ffdc has the value of 10;
after swap1 function,  a with adr ffda has the value of 10; b with addr ffdc has the value of 5;

Thanks

#include <stdio.h>

// Wrong

void swap(int x,int y)
{
int temp=x;
x=y;
y=temp;
}

//Swap: By swappings value presented by *x and *y

void swap1(int *x,int *y)
{
int temp=*x;
*x=*y;
*y=temp;
}

//Swap: By swapping pointers of x and y

void swap2(int *x,int *y)
{
int temp=&*x;
&*x=&*y;
&*y=temp;
}

main ()
{
int a=5;
int b=10;

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

swap(a,b);
printf("After simple swap, a=%d,b=%d\n",a,b);
printf("After simple swap, address of a=%x,b=%x\n",&a,&b);

swap1(&a,&b);
printf("After simple swap, a=%d,b=%d\n",a,b);
printf("After simple swap, address of a=%x,b=%x\n",&a,&b);.

swap2(&a,&b);
printf("After simple swap 2, a=%d,b=%d\n",a,b);
printf("After simple swap 2, address of a=%x,b=%x\n",&a,&b);
}
0
Question by:tiger0516
• 2

LVL 46

Accepted Solution

Kent Olsen earned 500 total points
ID: 16695280
Hi tiger0516,

Swap two doesn't work because you're not doing what you're thinking that you're doing.  :)  You may actually be swapping the pointers on the stack, but you're NOT swapping the pointers back in the main program.

Try this for main:

main ()
{
int a=5;
int b=10;
int  *aptr;
int  *bptr;
printf("Before swap, a=%d,b=%d\n",a,b);

swap(a,b);
printf("After simple swap, a=%d,b=%d\n",a,b);
printf("After simple swap, address of a=%x,b=%x\n",&a,&b);

swap1(&a,&b);
printf("After simple swap, a=%d,b=%d\n",a,b);
printf("After simple swap, address of a=%x,b=%x\n",&a,&b);.

aptr = &a;
bptr = &b;
swap2(&aptr,&bptr);
printf("After simple swap 2, a=%d,b=%d\n",a,b);
printf("After simple swap 2, address of a=%x,b=%x\n",&a,&b);
}

Then swap2 becomes:

void swap2(int **x,int **y)
{
int *temp;

temp = *x;
*x = *y;
*y = temp;
}

Good Luck!
Kent
0

LVL 1

Author Comment

ID: 16695321
BTW,

Should

void swap2(int **x,int **y)
{
int *temp;

temp = *x;
*x = *y;
*y = temp;
}

be

void swap2(int **x,int **y)
{
int *temp;

temp = **x;
**x =* *y;
**y = temp;
}

?
0

LVL 1

Author Comment

ID: 16695325
forgot to say thanks :-)
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
###### Suggested Courses
Course of the Month14 days, 17 hours left to enroll