Pointers in C: Defining a Pointer Variable

But enough talk—it’s time to see how pointers actually work. Suppose you define a variable called count as follows:

int count = 10;

You can define another variable, called int_pointer, that can be used to enable you to indirectly access the value of count by the declaration

int *int_pointer;

The asterisk defines to the C system that the variable int_pointer is of type pointer to int. This means that int_pointer is used in the program to indirectly access the value of one or more integer values.

You have seen how the & operator was used in the scanf calls of previous programs. This unary operator, known as the address operator, is used to make a pointer to an

object in C. So, if x is a variable of a particular type, the expression &x is a pointer to that variable. The expression &x can be assigned to any pointer variable, if desired, that has been declared to be a pointer to the same type as x.

Therefore, with the definitions of count and int_pointer as given, you can write a statement such as

int_pointer = &count;

to set up the indirect reference between int_pointer and count. The address operator has the effect of assigning to the variable int_pointer, not the value of count, but a pointer to the variable count. The link that has been made between int_pointer and count is conceptualized in Figure 11.1. The directed line illustrates the idea that int_pointer does not directly contain the value of count, but a pointer to the variable count.

To reference the contents of count through the pointer variable int_pointer, you use the indirection operator, which is the asterisk *. So, if x is defined as type int, the statement

x = *int_pointer;

assigns the value that is indirectly referenced through int_pointer to the variable x. Because int_pointer was previously set pointing to count, this statement has the effect of assigning the value contained in the variable count—which is 10—to the variable x.

The previous statements have been incorporated into Program 11.1, which illustrates the two fundamental pointer operators: the address operator, &, and the indirection oper- ator, *.

Program 11.1   Illustrating Pointers

// Program to illustrate pointers

#include <stdio.h>

int main (void)

{

int  count = 10, x;

int  *int_pointer;

int_pointer = &count;

x = *int_pointer;

printf (“count = %i, x = %i\n”, count, x);

return 0;

}

Program 11.1   Output

count = 10, x = 10

The variables count and x are declared to be integer variables in the normal fashion. On the next line, the variable int_pointer is declared to be of type “pointer to int.” Note that the two lines of declarations could have been combined into the single line

int count = 10, x, *int_pointer;

Next, the address operator is applied to the variable count. This has the effect of creating a pointer to this variable, which is then assigned by the program to the variable int_pointer.

Execution of the next statement in the program,

x = *int_pointer;

proceeds as follows: The indirection operator tells the C system to treat the variable int_pointer as containing a pointer to another data item. This pointer is then used to access the desired data item, whose type is specified by the declaration of the pointer variable. Because you told the compiler that int_pointer points to integers when you declared the variable, the compiler knows that the value referenced by the expression *int_pointer is an integer. And because you set int_pointer to point to the integer variable count in the previous program statement, it is the value of count that is indi- rectly accessed by this expression.

You should realize that Program 11.1 is a manufactured example of the use of point- ers and does not show a practical use for them in a program. Such motivation is present- ed shortly, after you have become familiar with the basic ways in which pointers can be defined and manipulated in a program.

Program 11.2 illustrates some interesting properties of pointer variables. Here, a pointer to a character is used.

Program 11.2   More Pointer Basics

// Further examples of pointers

#include <stdio.h>

int main (void)

{

char c = ‘Q’;

char *char_pointer = &c;

printf (“%c %c\n”, c, *char_pointer);

c = ‘/’;

printf (“%c %c\n”, c, *char_pointer);

*char_pointer = ‘(‘;

printf (“%c %c\n”, c, *char_pointer);

return 0;

}

Program 11.2   Output

Q Q

/ /

( (

The character variable c is defined and initialized to the character ‘Q’. In the next line of the program, the variable char_pointer is defined to be of type “pointer to char,” meaning that whatever value is stored inside this variable should be treated as an indirect reference (pointer) to a character. Notice that you can assign an initial value to this vari- able in the normal fashion. The value that you assign to char_pointer in the program is a pointer to the variable c, which is obtained by applying the address operator to the variable c. (Note that this initialization generates a compiler error if c had been defined after this statement because a variable must always be declared before its value can be ref- erenced in an expression.)

The declaration of the variable char_pointer and the assignment of its initial value could have been equivalently expressed in two separate statements  as

char *char_pointer;

char_pointer = &c;

(and not by the statements

char *char_pointer;

*char_pointer = &c;

as might be implied from the single-line declaration).

Always remember, that the value of a pointer in C is meaningless until it is set point- ing to something.

The first printf call simply displays the contents of the variable c and the contents of the variable that is referenced by char_pointer. Because you set char_pointer to point to the variable c, the value that is displayed is the contents of c, as verified by the first line of the program’s output.

In the next line of the program, the character ‘/’ is assigned to the character variable c. Because char_pointer still points to the variable c, displaying the value of *char_pointer in the subsequent printf call correctly displays this new value of c at the terminal. This is an important concept. Unless the value of char_pointer is changed, the expression *char_pointer always accesses the value of c. So, as the value of c changes, so does the value of *char_pointer.

The previous discussion can help you to understand how the program statement that appears next in the program works. Unless char_pointer is changed, the expression *char_pointer always references the value of c. Therefore, in the expression

*char_pointer = ‘(‘;

you are assigning the left parenthesis character to c. More formally, the character ‘(‘ is assigned to the variable that is pointed to by char_pointer.You know that this variable is c because you placed a pointer to c in char_pointer at the beginning of the pro- gram.

The preceding concepts are the key to your understanding of the operation of point- ers. Please review them at this point if they still seem a bit unclear.

Source: Kochan Stephen G. (2004), Programming in C: A Complete Introduction to the C Programming Language, Sams; Subsequent edition.

Leave a Reply

Your email address will not be published. Required fields are marked *