Let's break it down:
int a = 10, *p1, *p2; // nothing special
p1 = &a; // p1 now holds the address of a. printf("%d", *p1) would print 10, as it is the current value of a.
// in this point, printf("%d-%d", *p1,a); would print 10-10 (printf("%d",*p2); is UB as p2 is uninitialized)
*p1 = 25; // remember that p1 = &a, meaning that now a = 25. Basically you changed (the variable) a, using a pointer instead of changing it directly.
p2 = p1; // p2 now holds the value of p1, meaning it too points to a
// in this point, printf("%d-%d-%d", *p1,*p2,a); would print 25-25-25
*p2 = 12; // *p2 = 12, and so does *p1, and so does a
// in this point, printf("%d-%d-%d", *p1,*p2,a); would print 12-12-12
printf("%d", *p1);
You should remember that a is an int that holds an integer value, and p1,p2 are int * that hold the address of an int. After p1 = &a, every change to a would mean that *p1 is changed too, since *p1 is actually *(&a) [which is...a]. After p2 = p1, the same holds for p2.
I thought originally that code in general executes from top to bottom.
Well, it does :)
Answer from CIsForCookies on Stack OverflowBest Pointers Explanation
Dereferencing a char Pointer in C
pointers in C
Can someone explain pointers (in C) to me like I'm 10?
Videos
Let's break it down:
int a = 10, *p1, *p2; // nothing special
p1 = &a; // p1 now holds the address of a. printf("%d", *p1) would print 10, as it is the current value of a.
// in this point, printf("%d-%d", *p1,a); would print 10-10 (printf("%d",*p2); is UB as p2 is uninitialized)
*p1 = 25; // remember that p1 = &a, meaning that now a = 25. Basically you changed (the variable) a, using a pointer instead of changing it directly.
p2 = p1; // p2 now holds the value of p1, meaning it too points to a
// in this point, printf("%d-%d-%d", *p1,*p2,a); would print 25-25-25
*p2 = 12; // *p2 = 12, and so does *p1, and so does a
// in this point, printf("%d-%d-%d", *p1,*p2,a); would print 12-12-12
printf("%d", *p1);
You should remember that a is an int that holds an integer value, and p1,p2 are int * that hold the address of an int. After p1 = &a, every change to a would mean that *p1 is changed too, since *p1 is actually *(&a) [which is...a]. After p2 = p1, the same holds for p2.
I thought originally that code in general executes from top to bottom.
Well, it does :)
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
p1stores "1050";*p1, ie value stored at address stored insidep1, 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
p1stores "1050";*p1, ie value stored at address stored insidep1, is now 25
p2 = p1;
memory after the previous line
address | memory | variable
1050 | 25 | a
1054 | 1050 | p1
1058 | 1050 | p2
p1stores "1050";*p1, ie value stored at address stored insidep1, is 25- copy values stored inside
p1inp2; sop2points toa, 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:
p1andp2are pointers: they store address of variable&(like in&a): returns address of a variable*in declaration (like inint *p1): declare pointer to a variable (here to aintvariable)*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);
Could anyone recommend a video that provides a clear explanation of pointers in C programming? I've been struggling to understand them, and I'm looking for a resource that breaks down the concept effectively.