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 of &a variable: 1116119516
Address stored in p : 1116119516
Value of *p: 10
Address of &p : 1116119520
Address stored in p+1 variable: 1116119520
Address of &p+1 variable: 1116119528
value of *(p+1) variable: 1116119516
value of *p+1 variable: 11
Address stored in p+2 variable: 1116119524
Address of &p+2 variable: 1116119536
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