# Thread: Need Help Understand Pointers in C

1. ## Need Help Understand Pointers in C

Hello, New Member here !! I have just started Learning C after a long time and Started Pointers in C. I have some doubts in P+1 (Pointers arithmetic's) below i have given a example code
Code:
```#include <stdio.h>

int main ()
{
int  a = 10;   /* normal variable declaration */
int  *p;        /* pointer variable declaration */
p = &a;  /* store address of a in pointer variable*/
printf("Value of a variable: %d\n", a  );
printf("Address of &a variable: %d\n", &a  );
printf("\n");

printf("Address stored in p : %d\n",p );
printf("Value of *p: %d\n", *p );
printf("Address of &p variable: %d\n ", &p );
printf("\n");

printf("Address stored in p+1 variable: %d\n ", p+1);
printf("Address of &p+1 variable: %d\n ", &p+1 );
printf("value of *(p+1) variable: %d\n", *(p+1));
printf("value of *p+1 variable: %d\n", *p+1);
printf("\n");

printf("Address stored in p+2 variable: %d\n ", p+2);
printf("Address of &p+2 variable: %d\n ", &p+2);
printf("value of *(p+2) variable: %d\n", *(p+2));
printf("value of *p+2 variable: %d\n", *p+2);
return 0;
}```
OUTPUT :-
Code:
```~/Workspace/C\$ ./ pointers
Value of a variable: 10

Address stored in p : 1116119516
Value of *p: 10

Address stored in p+1 variable: 1116119520
value of *(p+1) variable: 1116119516
value of *p+1 variable: 11

Address stored in p+2 variable: 1116119524
value of *(p+2) variable: 32765
value of *p+2 variable: 12```
Attachment 16315

my doubt is, why Address of p (&p) and Address stored in p+1 is same. i know that by P+1 means is moving 4 bytes from the given address for integer which is p = 1116119516 + 4 --> P+1 = 1116119520, But &p is in 1116119520 before declaring P+1 which also gets 1116119520 as address. if both has same address wont there be some conflict in memory between those two if &p is called again.

doubt 2, Why is Address stored in p: 1116119516 and value of *(p+1) variable: 1116119516 is same. is't *(p+1) suppose to get some garbage value because its doesn't have a variable declared.

i don't understand this,
if made some mistake in explaining things forgive me and help clear my stupid doubt 2. why Address of p (&p) and Address stored in p+1 is same.
You may be confusing the value and address of a and p.

Code:
```   Value            Address
a  10               1116119516

p  1116119516       1116119520```
p+1 is the value of p plus 4 bytes. *(p+1) is the value of p, because you are dereferencing 1116119520 which is 1116119516.

The results can look weird at first glance because a and p are next to each other on the stack. That is an important thing to notice. 3. You're confused, because it's a crap example to work from to begin with.

The program is off in the weeds somewhere as soon as it starts doing anything with p+1

Draw out some memory diagrams like this.
Code:
```      +----------+
1000  |    10    | ;; a = 10
+----------+
1004  |   1000   | ;; p = &a
+----------+```
But the compiler could do this instead
Code:
```      +----------+
1000  |   1004   | ;; p = &a
+----------+
1004  |    10    | ;; a = 10
+----------+```
Or even this
Code:
```      +----------+
1000  |   1008   | ;; p = &a
+----------+
1004  |          | ;; invisible padding
+----------+
1008  |    10    | ;; a = 10
+----------+```
The point is, it's a crap-shoot as to what you'll see in any given implementation when you start accessing pointers out of bounds.

3 possible memory layouts, and the code is giving you 3 completely different answers, each with it's own unique 'voodoo' explanation. 4. If your still struggling to understand it try thinking of each variable (that is a and p) as a house, houses come in many sizes but always have an address and contents, the contents of house p is the address of house a, the &a/&p is saying give me the address of the house I'm pointing at, the *p is saying give follow the address contained in house p to house a and get the contents.

I don't think I missed anything there, gotta start work in a mo so I'll leave the rest to the other forum members 5. I would change only:
...the &a/&p is saying give me the address of the house I'm pointing at.
To
...the &a/&p is saying: give me the address of the house.
Is a is house, &a is the address of the house. Same to "house" p. 6. Originally Posted by flp1969 I would change only:

To

Is a is house, &a is the address of the house. Same to "house" p.
I said pointing with the image of the coder standing there literary pointing with their finger at the house, the & symbol being their finger Popular pages Recent additions address, pointer, pointer arithmetic, printf, variable 