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
%disn't correct for printing pointers - you want%p