Pointers as Return Values of Functions in C
In the previous lesson we saw how to pass pointers as function arguments. In this lesson we see how to return pointers from functions.
Pointers returned by functions
In the previous lesson we saw that it is possible to pass pointers as function arguments. With this technique it is possible to write functions that return multiple return values.
In this lesson we see how to implement functions that return pointers.
To do this we start from an example: we want to implement a function that, given two pointers to integers as input, returns the pointer to the minimum of the two.
A possible implementation is the following:
int *min(int *x, int *y) {
if (*x > *y) {
return y;
}
else {
return x;
}
}
To try using the function, we must first declare two variables and a pointer for the return value:
int *result;
int a, b;
printf("Insert two integer numbers:\n");
scanf("%d", &a);
scanf("%d", &b);
result = min(&a, &b);
By invoking the min function, x becomes to all effects an alias for a while y becomes an alias for b. If a is less than b then result will contain the address of a. Otherwise it will contain the address of b.
A function can also return the address of an external variable or a local variable provided it is declared as static.
Never return a pointer to a local variable
A very serious error that can be made in returning a pointer from a function is to return the address of a local variable:
int *function() {
int x;
/* ... */
return &x;
}
In the example above the problem is that we are returning the address of x. However, at the end of the function, x will cease to exist so the returned address will point to a memory location that is no longer valid.
In these cases compilers often show a warning message. For example gcc returns a message like this:
test_local_pointer.c: In function 'f':
test_local_pointer.c:5:12: warning: function returns address of local variable [-Wreturn-local-addr]
5 | return &x;
| ^~
Example: searching for the maximum of an array and its index
Let's now see an application example where a function returns a pointer.
We want to write a function, search_maximum, that searches for the maximum value of an array and returns its pointer. The function must also return the associated index.
We can write the search_maximum function in this way:
int *search_maximum(int a[], int n, int *i) {
int j;
*i = 0;
int max = a[0];
for (j = 1; j < n; ++j) {
if (a[j] > max) {
max = a[j];
*i = j;
}
}
return &a[*i];
}
In this example, the parameter i is used to store and return the index of the maximum value in the array. The local variable max is only used to search for the maximum value and therefore temporarily stores the maximum found so far.
It is interesting to observe the final return expression. In this case we are returning the address of the array location with index equal to *i.
A possible complete program that searches for the maximum and its index in an array of 10 elements is the following:
#include <stdio.h>
int *search_maximum(int a[], int n, int *i);
int main() {
const int n = 10;
int a[n];
int i;
int *max;
int maximum_index;
printf("Insert %d numbers:\n");
for (i = 0; i < n; ++i) {
scanf("%d", &a[i]);
}
max = search_maximum(a, n, &maximum_index);
printf("The maximum is %d and is located at position %d\n", *max, maximum_index);
return 0;
}
int *search_maximum(int a[], int n, int *i) {
int j;
*i = 0;
int max = a[0];
for (j = 1; j < n; ++j) {
if (a[j] > max) {
max = a[j];
*i = j;
}
}
return &a[*i];
}
Trying to compile and execute the program, a possible output is the following:
Insert 10 numbers:
23
12
78
33
22
94
32
102
54
57
72
The maximum is 102 and is located at position 7
In Summary
In this lesson we saw how to implement functions that return pointers. With this lesson we saw two possible uses of pointers.
Starting from the next lesson we will begin to study the close relationship between pointers and arrays.