?
Solved

Pointer question

Posted on 2006-05-16
3
Medium Priority
?
245 Views
Last Modified: 2010-04-15
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;

But swap2 does not work, I had though it will changes a's addr to ffdc, and b's to ffda. I made mistakes somewhere. Could you please help me ?

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);
      printf("Before swap, address of a=%x,b=%x\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
Comment
Question by:tiger0516
  • 2
3 Comments
 
LVL 46

Accepted Solution

by:
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);
     printf("Before swap, address of a=%x,b=%x\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

by:tiger0516
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

by:tiger0516
ID: 16695325
forgot to say thanks :-)
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

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…
Video by: Grant
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

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question