C Programming Absolute Beginner's Guide (3rd Edition)

(Romina) #1
FIGURE 24.1 The variable pAge points to age if pAge holds the address of age.

Warning

Just because you define two variables back to back doesn’t mean that C stores them
back to back in memory. C might store them together, but it also might not.

Warning

Never try to set the address of one type of variable to a pointer variable of a different
type. C lets you assign the address of one type of variable only to a pointer defined
with the same data type.

The isn’t part of a pointer variable’s name. You use the dereferencing operator for several
things, but in the pointer definition, the exists only to tell C that the variable is a pointer, not a
regular variable. The following four statements do exactly the same thing as the previous two
statements. Notice that you don’t use
to store the address of a variable in a pointer variable unless
you are also defining the pointer at the same time.


Click here to view code image


int age; // Defines a regular integer
int * pAge; // Defines a pointer to an integer
age = 19; //Stores 19 in age
pAge = &age; // Links up the pointer

Using the Dereferencing *


As soon as you link up a pointer to another variable, you can work with the other value by
dereferencing the pointer. Programmers never use an easy word when a hard one will do just as well

Free download pdf