You may recall that back in the lesson on functions we mentioned that there were two ways to get variable data back from a function. One way is through use of the array, and you should be right on the verge of guessing the other way. If your guess is through use of a pointer, you are correct. Load and display the program named twoway.c for an example of this.
main( )
{
int pecans,apples;
pecans = 100;
apples = 101;
printf("The starting values are %d %d\n",pecans,apples);
/* when we call "fixup" */
fixup(pecans,&apples); /* we take the value of pecans */
/* we take the address of apples */
printf("The ending values are %d %d\n",pecans,apples);
}
Pointers C Tutorial 8-4
fixup(nuts,fruit) /* nuts is an integer value */
int nuts,*fruit; /* fruit points to an integer */
{
printf("The value are %d %d\n",nuts,*fruit);
nuts = 135;
*fruit = 172;
printf("The values are %d %d\n",nuts,*fruit);
}
In twoway.c, there are two variables defined in the main program "pecans" and "apples". Notice that neither of these if defined as a pointer. We assign values to both of these and print them out, then call the function "fixup" taking with us both of these values. The variable "pecans" is simply sent to the function, but the address of the variable "apples" is sent to the function. Now we have a problem. The two arguments are not the same, the second is a pointer to a variable. We must somehow alert the function to the fact that it is supposed to receive an integer variable and a pointer to an integer variable. This turns out to be very simple. Notice that the parameter definitions in the function define "nuts" as an integer, and "fruit" as a pointer to an integer. The call in the main program therefore is now in agreement with the function heading and the program interface will work just fine.
In the body of the function, we print the two values sent to the function, then modify them and print the new values out. This should be perfectly clear to you by now. The surprise occurs when we return to the main program and print out the two values again. We will find that the value of pecans will be restored to its value before the function call because the C language makes a copy of the item in question and takes the copy to the called function, leaving the original intact. In the case of the variable "apples", we made a copy of a pointer to the variable and took the copy of the pointer to the function. Since we had a pointer to the original variable,
even though the pointer was a copy, we had access to the original variable and could change it in the function. When we returned to the main program, we found a changed value in "apples" when we printed it out.
By using a pointer in a function call, we can have access to the data in the function and change it in such a way that when we return to the calling program, we have a changed value of data. It must be pointed out however, that if you modify the value of the pointer itself in the function, you will have restored pointer when you return because the pointer you use in the function is a copy of the original. In this example, there was no pointer in the main program because we simply sent the address to the function, but in many programs you will use pointers in function calls. One of the places you will find need for pointers in function calls will be when you request data input using standard input/output routines. These will be covered in the next two chapters.
Compile and run twoway.c and observe the output.
main( )
{
int pecans,apples;
pecans = 100;
apples = 101;
printf("The starting values are %d %d\n",pecans,apples);
/* when we call "fixup" */
fixup(pecans,&apples); /* we take the value of pecans */
/* we take the address of apples */
printf("The ending values are %d %d\n",pecans,apples);
}
Pointers C Tutorial 8-4
fixup(nuts,fruit) /* nuts is an integer value */
int nuts,*fruit; /* fruit points to an integer */
{
printf("The value are %d %d\n",nuts,*fruit);
nuts = 135;
*fruit = 172;
printf("The values are %d %d\n",nuts,*fruit);
}
In twoway.c, there are two variables defined in the main program "pecans" and "apples". Notice that neither of these if defined as a pointer. We assign values to both of these and print them out, then call the function "fixup" taking with us both of these values. The variable "pecans" is simply sent to the function, but the address of the variable "apples" is sent to the function. Now we have a problem. The two arguments are not the same, the second is a pointer to a variable. We must somehow alert the function to the fact that it is supposed to receive an integer variable and a pointer to an integer variable. This turns out to be very simple. Notice that the parameter definitions in the function define "nuts" as an integer, and "fruit" as a pointer to an integer. The call in the main program therefore is now in agreement with the function heading and the program interface will work just fine.
In the body of the function, we print the two values sent to the function, then modify them and print the new values out. This should be perfectly clear to you by now. The surprise occurs when we return to the main program and print out the two values again. We will find that the value of pecans will be restored to its value before the function call because the C language makes a copy of the item in question and takes the copy to the called function, leaving the original intact. In the case of the variable "apples", we made a copy of a pointer to the variable and took the copy of the pointer to the function. Since we had a pointer to the original variable,
even though the pointer was a copy, we had access to the original variable and could change it in the function. When we returned to the main program, we found a changed value in "apples" when we printed it out.
By using a pointer in a function call, we can have access to the data in the function and change it in such a way that when we return to the calling program, we have a changed value of data. It must be pointed out however, that if you modify the value of the pointer itself in the function, you will have restored pointer when you return because the pointer you use in the function is a copy of the original. In this example, there was no pointer in the main program because we simply sent the address to the function, but in many programs you will use pointers in function calls. One of the places you will find need for pointers in function calls will be when you request data input using standard input/output routines. These will be covered in the next two chapters.
Compile and run twoway.c and observe the output.

