• C Programming Video Tutorials

Return a Pointer from a Function in C



In C programming, a function can be defined to have more than one argument, but it can return only one expression to the calling function.

A function can return a single value that may be any type of variable, either of a primary type (such as int, float, char, etc.), a pointer to a variable of primary or user−defined type, or a pointer to any variables.

Read this chapter to learn the different ways in which a function in a C program returns a pointer.

Return a Static Array from a Function in C

If a function has a local variable or a local array, then returning a pointer of the local variable is not acceptable because it points to a variable that no longer exists. Note that a local variable ceases to exist as soon as the scope of the function is over.

Example 1

The following example shows how you can use a static array inside the called function (arrfunction) and return its pointer back 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

Now consider the following function which will generate 10 random numbers. They are stored in a static array and return their pointer to the main() function. The array is then traversed in the main() function as follows −

#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

Run the code and check its 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. In the following example, we pass a string with a pointer, manipulate it inside the function, and return it to the main() function.

Example

Inside the called function, we use the malloc() function to allocate the memory. The passed string is concatenated with the local string before returning.

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

char *hellomsg(char *);

int main(){

   char *name = "TutorialsPoint";
   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

Run the code and check its output −

Hello TutorialsPoint

Return a Struct Pointer from a Function in C

The following example shows how you can return the pointer to a variable of struct type.

Here, the area() function has two call−by−value arguments. The main() function reads the length and breadth from the user and passes them to the area() function, which populates a struct variable and passes its reference (pointer) back to the main() function.

Example

Take a look at the program −

#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

When you run this code, it will produce the following output −

Length: 10.500000 
Breadth: 20.500000 
Area: 215.250000
Advertisements