Questions on pointers, dereferencing, and NULL


I have a few different questions on the chapter involving pointers. I understand the concept fine but I still have a few questions and maybe have a few things clarified…

I’ll start with the least important question. In the section titled “Getting Addresses” you have the following code:

int i = 17;
rintf(“i stores its value at %p\n”, &i);
printf(“this function starts at %p\n”, main);
return 0;

I understand that referencing variables addresses need the & symbol in front and function like main do not… But why? I’m not sure why main wouldn’t have to be &main or &someotherfunction like variables would to show their address in memory? Also (and more importantly) anything else not require the & for the purposes of displaying it’s address in memory?

The next question I have is just a bit of confusion on my part… When you assign NULL to a pointer is that referencing or dereferencing? Also, is my logic correct in the code below?

int i = 21;
int *myPointer = &i

myPointer = NULL; //assigns memory address from 0xWhatever to 0 … That wouldn’t make it’s address 0x0 would it?

If (myPointer) {
printf(“it is still holding an address\n”);
} else {
printf(“it is NULL there is no address stored in the pointer any longer\n”);

What happens if I did *myPointer = NULL; instead? I assume that i would = 0; ? Is = 0; the exact same thing as = NULL?



I don’t know about the main function question for sure, but I think I can help with the NULL one.

As far as I know, NULL isn’t the actual integer 0, but it is a setting that means something has no value. When forced to do something with NULL (or nil in Obj-C) you may get a representative value like 0 back, but it isn’t the literal value. In most languages sending messages to NULL will cause a crash, but in Obj-C it’s OK.


Thanks that is helpful. I guess I can just think of NULL as more like = “” rather than = 0 or = “0” then


main is a pointer. That is the whole story. (Now, the story may seem odd to you because you haven’t worked with pointers much, but that really is the whole story. Just keep going.)

If you do:

int i = 21; int *myPointer = &i *myPointer = NULL;
i will equal zero. (NULL is 0, you area storing it in the integer at myPointer, which is where i stores it data).


Why would someone need a pointer to nothing? Are you just holding that address in memory for something later at some other time?


Setting a pointer to NULL has a few advantages:
[li]If you accidentally try to use it, you’re more likely to get a useful error message and less likely to overwrite some unrelated piece of information in your application[/li]
[li]You can easily check to see if the pointer contains an address by comparing with NULL[/li]
[li]The line of code setting the pointer to NULL is nicely self-documenting: you’re explicitly declaring that you’re done with the pointer for now[/li]
[li]In a garbage collection environment, losing a pointer to an address tells the garbage collector that you’re no longer interested in that memory[/li][/ul]

There might be more advantages, those are just the ones that spring to mind.