A pointer represents both the address and the type of an object or function. If an object or function has the type T, then a pointer to it has the derived type pointer to T. For example, if var is a float variable, then the expression &var whose value is the address of the float variable has the type pointer to float, or in C notation, the type float *. A pointer to any type T is also called a T pointer for short. Thus the address operator in &var yields a float pointer.
Because var doesn’t move around in memory, the expression &var is a constant pointer. However, C also allows you to define variables with pointer types. A pointer variable stores the address of another object or a function. We describe pointers to arrays and functions a little further on. To start out, the declaration of a pointer to an object that is not an array has the following syntax:
type * [type-qualifier-list] name [= initializer];
In declarations, the asterisk (*) means “pointer to.” The identifier name is declared as an object with the type type *, or pointer to type. The optional type qualifier list may contain any combination of the type qualifiers const, volatile, and restrict.
Here is a simple example:
int *iPtr; // Declare iPtr as a pointer to int.
The type int is the type of object that the pointer iPtr can point to. To make a pointer refer to a certain object, assign it the address of the object. For example, if iVar is an int variable, then the following assignment makes iPtr point to the variable iVar:
iPtr = &iVar; // Let iPtr point to the variable iVar.
The general form of a declaration consists of a comma-separated list of declarators, each of which declares one identifier. In a pointer declaration, the asterisk (*) is part of an individual declarator. We can thus define and initialize the variables iVar and iPtr in one declaration, as follows:
int iVar = 77, *iPtr = &iVar; // Define an int variable and a pointer to it.