0

In the code I wrote:

   int *p1=&a,**p3=&p1;

after passing p3 as an argument to the function 'change', p1 gets changed...then the main function has printed p1, p1 takes the value at double pointer p3(as wrote int **p3=&p1), but it does not take address of a(int *p1=&a).

This means that value of p1 will be the value at p3, not the address of variable 'a'? Why the value at double pointer 'p3' will be considered??

Why not the value of address(&a) which the pointer stored? can anyone clarify these two lines....

   int *p1=&a;

   int **p3=&p1;

I understood the concept of double pointers, but need clarification regarding these two lines...

#include<stdio.h>   
void main()
{
    int a=2,b=3;
    int *p1=&a,*p2=&b,**p3=&p1;
    printf("p1=%d, p2=%d, p3=%d\n",p1,p2,p3);
    change(p3,p2);
    printf("p1=%d, p2=%d, p3=%d\n",p1,p2,p3);
    printf("p1=%d\n",p1);
    printf("a=%d, b=%d\n",*p1,*p2);
    }
    void change(int **x,int *z)
    {
    *x=z;
     printf("p1=%d, p2=%d\n",*x,z);
     printf("a=%d, b=%d\n",**x,*z);
}

output:

   p1=-840577016,p2=-840577012,p3=-840577008

   p1=-840577012,p2=-840577012

   a=3,b=3

   p1=-840577012,p2=-840577012,p3=-840577008

   p1=-840577012

   a=3,b=3
5
  • 3
    The output “a=3,b=3” could not possibly have been produced by the statement “printf("a=%d, b=%d\n",*p1,*p2);”, as the latter contains a space and the former does not. While that does not cause a problem in this case, when showing output, please copy and paste the exact output, to avoid errors in transcription. Commented Dec 15, 2017 at 16:07
  • %d isn't correct for printing pointers - you want %p Commented Dec 15, 2017 at 16:14
  • @ChrisTurner.: Already edited in answer before you commented Commented Dec 15, 2017 at 16:15
  • p1=1244434040, p2=1244434044, p3=1244434048 p1=1244434044, p2=1244434044 a=3, b=3 p1=1244434044, p2=1244434044, p3=1244434048 p1=1244434044 a=3, b=3 Commented Dec 15, 2017 at 16:16
  • A pointer is nothing more than a variable that stores the address of something else as its value. While you can continue adding levels of indirection to any pointer as an educational exercise, that isn't the intended purpose. Multiple levels of indirection are a tool that make it possible to index individual pointers within a collection, not simply continually dereference pointers to get to the value at a single address. The exercise is fine, just be aware that a pointer to pointer to type (e.g. a double-pointer) is generally used to access 1 in a collection of pointers. Commented Dec 15, 2017 at 16:48

1 Answer 1

4

Because you changed p1 in this line. *x=z;

To explain a bit more you have done this

change(p3,p2)

Now p3 has address of p1. And then you just changed it by assigning to it something else..

That something else is nothing but b's address.

void change(int **x,int *z)

x contains address of p1. z contains address of b.

Then you said that whatever is the content of x - go there and write there whatever is the content of z. z contained address of b. So now we changed p1-s content to the address of b.

..but in the main will it ignore the line int *p1=&a after p1 gets changed in the function??

Well to the OP - do you think that p1 will get changed here?

int *p1 = &a;
     p1 = &b;

We have lastly assigned b's address to p1. The value earlier written os overwritten.

Nothing is ignored - the program did whatever it is said to do.

Another point

Use %p to print a pointer like this

printf("p1=%p, p2=%p, p3=%p\n", (void*)p1, (void*)p2, (void*)p3);
Sign up to request clarification or add additional context in comments.

3 Comments

yes i have changed it in the function...but in the main will it ignore the line int *p1=&a after p1 gets changed in the function??
@vyshnavivanjari - What do you mean by "ignore"? Nothing has been ignored - as you said, you've changed it.
@coderredoc ...understood..it was os overwritten...thanks a lot

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.