Originally Posted by
Hodor
p holds the "memory location" of the structure, not member x of the structure belonging to the structure being pointed to
(better to think of p as pointing to a structure than being a memory location, but.. yeah)
Output of program
address of p : 00000000
address of p : 007113A8
address of p->x : 007113A8
value of p->x : A
value of p->x : B
address of p : 007113A8
address of p->x : 007113A8
address of p : 007113A8
Code:
#include<stdio.h>
#include<stdlib.h>
struct point //define structure
{
char x; //structure member type integer
};
void main ()
{
struct point *p = NULL;
printf("address of p : %p \n", (void*)p);
p = malloc(sizeof(*p));
printf("address of p : %p \n", (void*)p);
printf("address of p->x : %p \n", (void*)&p->x);
p->x = 'A';
printf("value of p->x : %c \n", p->x);
p->x = 'B';
printf("value of p->x : %c \n", p->x);
printf("address of p : %p \n", (void*)p);
printf("address of p->x : %p \n", (void*)&p->x);
free(p);
printf("address of p : %p \n", (void*)p);
}
first time assigning 'A'
Memory location = 007113A8 = A
second time assigning 'B'
Memory location = 007113A8 = B
only value will change but address of pointer will not change