Comment

Pointers & Pointers

pointer pa pointing to int a

There are two concepts:

pointer variable — A pointer variable is a variable that is expected to hold the address of another entity in main memory. The variable pa in the picture is a pointer variable. Often a pointer variable is called a "pointer".
pointer value — A pointer value is the address of some entity in main memory. The black dot and arrow in the picture represents a pointer value. Often a pointer value is called a "pointer".

In somewhat casual discussion, the word "pointer" can mean either of the above two things. Be careful. In the picture there are four things: the pointer variable, the pointer value in it, the variable pointed to, and what the pointed-to variable contains.

Every byte in main memory has an address. So, every entity (variable, array, struct, or function) in main memory has an address. You can declare a pointer variable for each of these types. For example, you can have a pointer variable that contains the addess of an int variable (as in the picture.)

Usually, a pointer variable only points at one type. The puzzles in this section mostly use integers and pointers to integers, but this is for practice.

Any variable (or other entity) can be pointed to. The declaration of the variable does not indicate if it will be pointed to. Inspect the following:

int x;
int y;

int *ptr;

There are three variables (little blocks of memory), none of them initialized. The variable ptr has the potential to point to either int variable. Nothing in the code so far ties it to either.



Next Page         Previous Page Home