Pointers and Functions 255
test (p);
printf ("After the call to test i = %i\n", i);
return 0;
}
Program 11.8 Output
Before the call to test i = 50
After the call to test i = 100
The function testis defined to take as its argument a pointer to an integer. Inside the
function, a single statement is executed to set the integer pointed to by int_pointerto
the value 100.
The mainroutine defines an integer variable iwith an initial value of 50 and a point-
er to an integer called pthat is set to point to i.The program then displays the value of
iand calls the testfunction, passing the pointer pas the argument. As you can see from
the second line of the program’s output, the testfunction did, in fact, change the value
of ito 100.
Now consider Program 11.9.
Program 11.9 Using Pointers to Exchange Values
// More on pointers and functions
#include <stdio.h>
void exchange (int const pint1, int const pint2)
{
int temp;
temp = pint1;
pint1 = pint2;
pint2 = temp;
}
int main (void)
{
void exchange (int const pint1, int const pint2);
int i1 = -5, i2 = 66, p1 = &i1, p2 = &i2;
printf ("i1 = %i, i2 = %i\n", i1, i2);
Program 11.8 Continued