Returning a Pointer from a Function in C
In C programming, functions can return a single value, including a pointer to a variable of a primary or user-defined type. This chapter explores various methods for returning pointers from functions, enhancing your understanding of memory management and function design in C.
Return a Pointer from a Function in C
In C programming, a function can have multiple arguments, but it can return only one expression to the calling function. This return value can be of any type, including a pointer to a variable of a primary or user-defined type.
In this tutorial, we will explore different ways in which a C function can return a pointer.
Return a Static Array from a Function in C
If a function has a local variable or a local array, returning a pointer to that local variable is not acceptable because it points to a variable that no longer exists once the function's scope ends.
Example 1
The following example demonstrates how to use a static array inside the called function (arrfunction
) and return its pointer to the main()
function.
#include <stdio.h>
#include <math.h>
float * arrfunction(int);
int main() {
int x = 100, i;
float *arr = arrfunction(x);
printf("Square of %d: %f\n", x, *arr);
printf("Cube of %d: %f\n", x, arr[1]);
printf("Square root of %d: %f\n", x, arr[2]);
return 0;
}
float *arrfunction(int x) {
static float arr[3];
arr[0] = pow(x, 2);
arr[1] = pow(x, 3);
arr[2] = pow(x, 0.5);
return arr;
}
Output
When you run this code, it will produce the following output:
Square of 100: 10000.000000 Cube of 100: 1000000.000000 Square root of 100: 10.000000
Example 2
Consider the following function, which generates 10 random numbers stored in a static array and returns a pointer to the main()
function:
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
/* function to generate and return random numbers */
int *getRandom() {
static int r[10];
srand((unsigned)time(NULL)); /* set the seed */
for(int i = 0; i & 10; ++i) {
r[i] = rand();
}
return r;
}
int main() {
int *p; /* a pointer to an int */
p = getRandom();
for(int i = 0; i & 10; i++) {
printf("*(p + %d): %d\n", i, *(p + i));
}
return 0;
}
Output
*(p + 0): 776161014 *(p + 1): 80783871 *(p + 2): 135562290 *(p + 3): 697080154 *(p + 4): 2064569097 *(p + 5): 1933176747 *(p + 6): 653917193 *(p + 7): 2142653666 *(p + 8): 1257588074 *(p + 9): 1257936184
Return a String from a Function in C
Using the same approach, you can pass and return a string to a function. A string in C is an array of char type. The following example shows how to pass a string with a pointer, manipulate it inside the function, and return it to the main()
function.
Example
In this example, we use malloc()
to allocate memory for the string. The passed string is concatenated with a local string before returning.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
char *hellomsg(char *);
int main() {
char *name = "TutorialsArena";
char *arr = hellomsg(name);
printf("%s\n", arr);
return 0;
}
char *hellomsg(char *x) {
char *arr = (char *)malloc(50 * sizeof(char));
strcpy(arr, "Hello ");
strcat(arr, x);
return arr;
}
Output
Hello TutorialsArena
Return a Struct Pointer from a Function in C
The following example shows how to return a pointer to a variable of struct type. Here, the area()
function takes two call-by-value arguments. The main function reads the length and breadth from the user, passes them to the area()
function, which populates a struct variable and returns its reference (pointer) back to the main function.
Example
#include <stdio.h>
#include <string.h>
struct rectangle {
float len, brd;
double area;
};
struct rectangle *area(float x, float y);
int main() {
struct rectangle *r;
float x, y;
x = 10.5, y = 20.5;
r = area(x, y);
printf("Length: %f \nBreadth: %f \nArea: %lf\n", r->len, r->brd, r->area);
return 0;
}
struct rectangle *area(float x, float y) {
double area = (double)(x * y);
static struct rectangle r;
r.len = x; r.brd = y; r.area = area;
return &r;
}
Output
Length: 10.500000 Breadth: 20.500000 Area: 215.250000