Pointers

A pointer represents the address and type of a variable or a function. In other words, for a variable x, &x is a pointer to x.

A pointer refers to a location in memory, and its type indicates how the data at this location is to be interpreted. Thus the pointer types are called pointer to char, pointer to int, and so on, or for short, char pointer, int pointer, etc.

Array names and expressions such as &x are address constants or constant pointers, and cannot be changed. Pointer variables, on the other hand, store the address of the object to which they refer, which address you may change. A pointer variable is declared by an asterisk (*) prefixed to the identifier. For example:

float  x, y, *pFloat;
pFloat = &x;     // Let pFloat point to x.

After this declaration, x and y are variables of type float, and pFloat is a variable of type float * (pronounced “pointer to float“). After the assignment operation, the value of pFloat is the address of x.

The indirection operator * is used to access data by means of pointers. If ptr is a pointer, for example, then *ptr is the object to which ptr points. For example:

y = *pFloat;   //  equivalent to  y = x;

As long as pFloat points to x, the expression *pFloat can be used in place of the variable x. Of course, the indirection operator * must only be used with a pointer which contains a valid address.

A pointer with the value 0 is called a null pointer . Null pointers have a special significance in C. Because all objects and functions have ...

Get C Pocket Reference now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.