MoMoe0 MoMoe0 - 1 year ago 100
C Question

Printing a null pointer with printf gives (nil) but printing &ptr gives an address. Why?

So if I do:

int *ptr = NULL:

printf("%p", ptr);

I get (nil) printed out. So this is printing out the address of the pointer? Which points to a null pointer address.

but if I do:

int *ptr = NULL;

printf("%p", &ptr);

I get a memory address. Why is this? Is it because it's giving where the actual NULL memory location is? Does this location always change? I keep getting different memory addresses whenever I use the second piece of code.

Answer Source

The value stored in the pointer ptr is NULL or 0.

However, ptr is a variable. It is stored in some memory location. So if you take &ptr you will get the memory location that ptr is stored in.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download