Pointers

A pointer type variable holds the address of a data object or function. A pointer can refer to an object of any one data type, but cannot point to a bit field or to an object having the register storage class specifier. Some common uses for pointers are:

 

Declaring a Pointer
A pointer is declared by placing an * (asterisk) after the data type specifier and before the identifier. The following example declares pcoat as a pointer to an object having type long:

extern long *pcoat;

If the keyword volatile appears before the *, the declarator describes a pointer to a volatile object. If the keyword volatile comes between the * and the identifier, the declarator describes a volatile pointer. The keyword const operates in the same manner as the volatile keyword described. In the following example, pvolt is a constant pointer to an object having type short:

short * const pvolt;

The following example declares pnut as a pointer to an int object having the volatile qualifier:

extern int volatile *pnut;

The following example defines psoup as a volatile pointer to an object having type float:

float * volatile psoup;

The following example defines pfowl as a pointer to an enumeration object of type bird:

enum bird *pfowl;

The next example declares pvish as a pointer to a function that takes no parameters and returns a char object:

char (*pvish)(void);

 

Assigning Pointers
When you use pointers in an assignment operation, you must ensure that the types of the pointers in the operation are compatible.

The following example shows compatible declarations for the assignment operation:

float subtotal;
float * sub_ptr;
      .
      .
      .
sub_ptr = &subtotal;
printf("The subtotal is %f\n", *sub_ptr);

The next example shows incompatible declarations for the assignment operation:

double league;
int * minor;
      .
      .
      .
minor = &league;     /* error */

 

Initializing Pointers
The initializer is an = (equal sign) followed by the expression that represents the address that the pointer is to contain. The following example defines the variables time and speed as having type double and amount as having type pointer to a double. The pointer amount is initialized to point to total:

double total, speed, *amount = &total;

The compiler converts an unsubscripted array name to a pointer to the first element in the array. You can assign the address of the first element of an array to a pointer by specifying the name of the array. The following two sets of definitions are equivalent. Both define the pointer student and initialize student to the address of the first element in section:

int section[80];
int *student = section;

is equivalent to:

int section[80];
int *student = &section[0];

You can assign the address of the first character in a string constant to a pointer by specifying the string constant in the initializer.

The following example defines the pointer variable string and the string constant "abcd". The pointer string is initialized to point to the character a in the string "abcd".

char *string = "abcd";

The following example defines weekdays as an array of pointers to string constants. Each element points to a different string. The pointer weekdays[2], for example, points to the string "Tuesday".

static char *weekdays[ ] =
            {
              "Sunday", "Monday", "Tuesday", "Wednesday",
              "Thursday", "Friday", "Saturday"
            };

A pointer can also be initialized to NULL using any integer constant expression that evaluates to 0, for example char * a=0;. Such a pointer is a NULL pointer. It does not point to any object.

 

Using Pointers
Two operators are commonly used in working with pointers, the address (&) operator and the indirection (*) operator. You can use the & operator to refer to the address of an object. For example, the following statement assigns the address of x to the variable p_to_x. The variable p_to_x has been defined as a pointer.

int x, *p_to_x;
 
p_to_x = &x;

The * (indirection) operator lets you access the value of the object a pointer refers to. The following statement assigns to y the value of the object that p_to_x points to:

float y, *p_to_x;
  .
  .
  .
y = *p_to_x;

The following statement assigns the value of y to the variable that *p_to_x references:

char y ,
     *p_to_x,
  .
  .
  .
*p_to_x = y;

You cannot use pointers to reference bit fields or objects having the register storage class specifier.

 

Pointer Arithmetic
You can perform a limited number of arithmetic operations on pointers. These operations are:

The increment (++) operator increases the value of a pointer by the size of the data object the pointer refers to. For example, if the pointer refers to the second element in an array, the ++ makes the pointer refer to the third element in the array.

The decrement (--) operator decreases the value of a pointer by the size of the data object the pointer refers to. For example, if the pointer refers to the second element in an array, the -- makes the pointer refer to the first element in the array.

You can add a pointer to an integer, but you cannot add a pointer to a pointer.

If the pointer p points to the first element in an array, the following expression causes the pointer to point to the third element in the same array:

p = p + 2;

If you have two pointers that point to the same array, you can subtract one pointer from the other. This operation yields the number of elements in the array that separate the two addresses that the pointers refer to.

You can compare two pointers with the following operators: ==, !=, <, >, <=, and >=.

Pointer comparisons are defined only when the pointers point to elements of the same array. Pointer comparisons using the == and != operators can be performed even when the pointers point to elements of different arrays.

You can assign to a pointer the address of a data object, the value of another compatible pointer or the NULL pointer.

 

Passing Pointer Values to Functions
Pointers allow a called function to alter the value of a variable in the calling function. Any changes to a variable passed as an argument to a called function are not returned to the calling function. However, if a pointer to a variable is passed as an argument, the called function can alter the value of the variable the pointer refers to.



Declarators
Initializers


Examples of Pointer Declaration and Use


Address (&) Operator
Indirection (*) Operator
Data Qualifiers
Pointers to Functions
Pointers to Members
Pointers to void