int a = 10;
int *p = &a;
*p = 20; /* Is this a valid statement? */
I understand that if I do int *p; and then if I do *p = 10 , it is invalid because I have not assigned any memory to p. However, I was wondering if initializing a pointer to some address allocates memory to that pointer or not?
The answer is
YESin this particular case.But when you do like this :
But actually
ptris not pointing to anywhere, basically it means it hasindeterminate value.so doing
*ptr = 20will put the value 20 to the memory address pointed to byptr.So it’s called
Undefined bahaviorIn you case it’s valid because
&ais valid memory location andpstarted pointing to that variable when we dop= &a.so
*p = 20means actually changing or assigning the value ofausing the pointerp.