C Programming: How to Use Pointers in Functions

By Dan Gookin

A pointer is a type of variable. As such, it can easily be flung off to a function in C programming. Even more thrilling, a pointer can wander back from a function as a return value. Oftentimes, these tricks are the only ways to get information to or from to a function.

How to pass a pointer to a function

The great advantage of passing a pointer to a function is that the information that’s modified need not be returned. That’s because the function references a memory address, not a value directly. By using that address, information can be manipulated without being returned. Pointing at a Discount demonstrates.


#include <stdio.h>
void discount(float *a);
int main()
 float price = 42.99;
 printf("The item costs $%.2fn",price);
 printf("With the discount, that's $%.2fn",price);
void discount(float *a)
 *a = *a * 0.90;

In Line 3 of Pointing at a Discount, the discount() function is prototyped. It requires a float type of pointer variable as its only argument.

Line 10 passes the address of the price variable to the discount() function. The percent sign obtains the memory location of the price variable.

Within the function, pointer variable a is used to peek at the value at the memory location that’s passed.

Exercise 1: Type the source code from Pointing at a Discount into your editor. Build and run the program.

Exercise 2: Modify your source code from Exercise 1 so that a float pointer variable p is declared in the main() function. Initialize p to the price variable’s location, and then pass p to the discount() function.

Exercise 3: Build a new project with two functions: create() and show(). The create() function receives a pointer to an array of ten integers and fills that array with random values in the range of 0 through 9. The show() function receives the same array and displays all ten elements.

How to return a pointer from a function

Functions are known by their types, such as int or char or even void. You can also declare pointer functions, which return a memory location as a value. Simply declare the function as being of a pointer type, such as

char *monster(void)

In this example, the monster() function is declared. It requires no arguments but returns a pointer to a char array — a string value.

Here’s another difference with functions that return pointers: The value that’s returned must be declared as a static variable. Keep in mind that variables are local to their functions. You must retain the data in the variable by declaring it as a static type so that its contents aren’t discarded when the function stops. Reversing a String provides an example.


#include <stdio.h>
char *strrev(char *input);
int main()
 char string[64];
 printf("Type some text: ");
char *strrev(char *input)
 static char output[64];
 char *i,*o;
 i=input; o=output;
 while(*i++ != 'n')
 while(i >= input)
 *o++ = *i--;
 *o = '';

Reversing a String can get quite confusing. Pay attention!

Line 3 prototypes the strrev() function. It returns a pointer — in this case, the address of a char array or string.

The main() function at Line 5 is pretty easy to figure out. Input is gathered by the fgets() function at Line 10. It’s passed to strrev() at Line 11 inside the puts() function.

The strrev() function begins at Line 16. It requires a char pointer as its argument, which is referred to as input in the function. The output buffer is created at Line 18, and it’s static, so it doesn’t go away when the function is done. Line 19 declares two char pointers: i and o.

The first while loop at Line 23 finds the newline character at the end of the input string. The i variable marches through the string one character at a time.

After finding the newline, the i pointer contains the address of the next character in input, which is probably not what you want. So the statement at Line 25 backs up i to point at the last character in the string before the newline.

At the start of the while loop at Line 27, pointer o holds the base of the output buffer, the first character, and pointer i holds the last. Try to think of this situation as i standing at the top of a staircase and o standing at the bottom.

The while loop spins until the address in pointer i matches the address at the start of the input string. As i is decremented, the characters at address i are copied to address o. Figuratively, i marches down the stairs, and o marches up.

Line 29 caps the output string with a NULL character. That’s something you must remember when you create strings by using pointers.

The return statement at Line 31 sends the address of the output buffer, the reversed string, back to the calling statement.

Exercise 4: Type the source code from Reversing a String into your editor. As you type the code, add your own comments describing what’s going on. Feel free to use the text as a guide. Build and run the program.