Passing Pointers to Functions

Summary: A function that receives pointers can access variables that are local to other functions.

One of the most common uses of pointers is to pass them as arguments to functions. Functions that receive variables as parameters get local copies of those variables, not the originals. In contrast, functions that receive pointers to variables gain access to the original variables associated with the pointers. This allows the functions to

Return more than one value

Read and change values in variables—including arrays and structures—that otherwise aren't visible to the function

The first item listed above relates to the return statement. As we noted in Chapter 2, “Functions,” a function can return only one value through return. How-ever, it's not difficult to imagine a useful function—a sort, for instance—that would return more than one value. Pointers offer an elegant solution.

The second item involves visibility. Most variables in C programs are local to the functions where they are defined, and a function normally can't access local variables in other functions. There are times, however, when you want a function to have access to a local variable defined elsewhere in the program. By passing the function a pointer to the local variable, you can give it access to the variable itself.

The PFUNC.C program illustrates both ideas. It has a function that returns more than one value and uses pointers to alter variables that aren't visible within the function:

/* PFUNC.C: Pass pointers to a function. */

#include <stdio.h>

void swap( int *ptr1, int *ptr2 );

main()

{

int first = 1, second = 3;

int *ptr = &second;

printf( "first: %d second: %d\n", first, *ptr );

swap( &first, ptr );

printf( "first: %d second: %d\n", first, *ptr );

}

void swap( int *ptr1, int *ptr2 )

{

int temp;

temp = *ptr1;

*ptr1 = *ptr2;

*ptr2 = temp;

}

Here is the output from PFUNC.C:

first: 1 second: 3

first: 3 second: 1

Summary: Pointers can eliminate the need for external variables.

The PFUNC.C program swaps the values of two int variables named first and second, using a function named swap. Since the exchange involves two values, the swap function can't use return to communicate its results. More-over, the variables first and second are defined only in the main function, and as good C programmers, we want to exchange their values without making them externally visible.

The prototype for the swap function shows that swap expects to receive two pointers to int variables:

void swap( int *ptr1, int *ptr2 );

Notice the use of void in the prototype and function definition. The void specifier shows that the swap function doesn't return any value through a return statement. Instead, swap returns its results indirectly, through the action of pointers.

The variables we want to exchange are defined only in main:

int first = 1, second = 3;

No other function in the program can access these variables directly by using the variable names first and second. We must pass these variables as arguments; but since the C language passes arguments by value, we need to pass pointers to the variables.

The main function calls swap with the following statement:

swap( &first, ptr );

This statement shows two different ways to pass a pointer to a function. The first argument in the function call,

&first

passes the address of first as a constant, using the address-of operator. The second argument,

ptr

passes the address of second with a pointer variable. Earlier in PFUNC.C we declared ptr as a pointer to an int and assigned it the address of second:

int *ptr = &second;

Both arguments pass the same kind of data—the address of a local variable—to the function. We'll return to this idea after we see how the rest of PFUNC.C works.

When the swap function executes, it creates two int pointers named ptr1 and ptr2 and assigns the passed addresses to them:

void swap( int *ptr1, int *ptr2 )

Since there's a one-to-one correspondence between arguments and parameters, the pointer ptr1 receives the address of first and ptr2 receives the address of second. The swap function exchanges the values of first and second, using the two pointers and a temporary int variable named temp:

int temp;

temp = *ptr1;

*ptr1 = *ptr2;

*ptr2 = temp;

Within the swap function, PFUNC.C uses the indirection operator to access the values that ptr1 and ptr2 reference. The expression *ptr1 accesses the value stored in first. Likewise, the expression *ptr2 accesses the value stored in second.

Through the addresses contained in the pointers, the swap function can indirectly access variables that are local to the main function.