Search code examples
cpointerstheorydereferencelvalue

Would these C-pointer operations cause problems?


Let's say I've got that:

char *p = NULL;

I'm sure this one would be a problem as I'd be dereferencing a NULL pointer:

*p = 16;

On the other hand, I think this one would be OK for I'd be getting the address of *p which is not NULL itself:

char **pp = &p;

Am I right in both cases?


Solution

  • Yes you are. Although the value of p is NULL, it still has a valid address, so you may pass its reference.