Address values used here is totally arbitrary, just for example
int a = 10, *p1, *p2;
the previous line declare one variable of type int (a) and two pointers to int (p1 and p2)
memory after the previous line
address | memory | variable
1050 | 10 | a
1054 | xxx | p1
1058 | xxx | p2
p1 = &a; // &a is address of a, ie here, 1050
memory after the previous line
address | memory | variable
1050 | 10 | a
1054 | 1050 | p1
1058 | xxx | p2
p1 stores "1050"; *p1, ie value stored at address stored inside p1, is 10
*p1 = 25; // *p1 means value stored at address stored inside p1
memory after the previous line
address | memory | variable
1050 | 25 | a
1054 | 1050 | p1
1058 | xxx | p2
p1 stores "1050"; *p1, ie value stored at address stored inside p1, is now 25
p2 = p1;
memory after the previous line
address | memory | variable
1050 | 25 | a
1054 | 1050 | p1
1058 | 1050 | p2
p1 stores "1050"; *p1, ie value stored at address stored inside p1, is 25
- copy values stored inside
p1 in p2; so p2 points to a, ie stores address "1050"
*p2 = 12;
memory after the previous line
address | memory | variable
1050 | 12 | a
1054 | 1050 | p1
1058 | 1050 | p2
printf("%d", *p1); // Print value stored at address stored inside p1
What we can see here:
p1 and p2 are pointers: they store address of variable
& (like in &a): returns address of a variable
* in declaration (like in int *p1): declare pointer to a variable (here to a int variable)
* in expression (like in *p1 = 25): access to value stored at address stored in pointer
You can see different addresses and values :
printf("address of a: %p\n", &a);
printf("address of p1: %p\n", &p1);
printf("address of p2: %p\n", &p2);
// address stored inside p1 (ie value stored inside p1)
printf("address stored inside p1: %p\n", p1);
// address stored inside p2 (ie value stored inside p2)
printf("address stored inside p2: %p\n", p2);
printf("value of a: %d\n", a);
printf("value pointed by p1: %d\n", *p1);
printf("value pointed by p2: %d\n", *p2);
25?p2 = p1both pointers point to the same adress with the same value. It does not matter with which pointer do you change the memory. From that line onp1anp2always point to the same value.25?