Function Returning Pointer in C Programming Language

Function-Returning-Pointer-in-C-Programming-Language

Function Returning Pointer in C:

Write a Program to demonstrate a function returning pointer in c programming language. As we have discussed earlier in the introduction to pointer and pointers and array articles, We can return a pointer variable from a function in C language. We are going to look at such an example in this article.

When we want to return multiple values or an array from a function, we can accomplish this by returning the array as a pointer variable.

We need to use a static variable(static array) inside the function so that it will be live throughout the life of the program.

Program Prerequisites:

Please go through the following pointers and array articles to better understand the program.

Let’s look at the step-by-step explanation of the program, where a function returns a pointer variable in c language.

Function Returning Pointer in C Program Explanation:

We are going to define a function called generateRandomArray() to generate an array with random integer numbers. This function takes the size as the input and generates an array with size number of random elements.

The generateRandomArray() function also returns the generated array as an integer pointer( int *) – i.e the generateRandomArray() function returns an integer pointer back to the caller.

Here is the prototype of the generateRandomArray() function

int * generateRandomArray(int size)

We use the rand() function from the stdlib.h header file to generate the random elements. Here is the syntax of the rand() function.

int rand(void)

We pass the time(0) to the srand() function(seed) to generate random numbers. The time(0) is equal to the number of seconds elapsed from the epoch (  00:00:00 UTC, January 1, 1970.). If you don’t pass the random seed to the srand() function, Then there is a possibility of getting the same values every time you run the program.

srand(time(0));

Store the random values on a static integer array called randArray.

static int randArr[MAXSIZE];

Why We need to use a Static array:

The default storage class for local variables is automatic, and they will be destroyed once the function returns. Because we need our random array (  randArr ) to be available even after the  generateRandomArray() function returns, So we must use a static integer array that will be alive throughout the program’s life.

Inside the main() function, Prompt the user to provide the desired size of array and update the input value in the size variable.

Finally, Call the generateRandomArray function with the size as the parameter. As the generateRandomArray() function returns an integer pointer, so store the return value in the integer pointer ptr.

ptr = generateRandomArray(size);

Print the array elements by iterating over the ptr using a for loop.

Program to demonstrate Function Returning Pointer in C:

We have included detailed comments in the program for your convenience.

Note that, the randArr is a static array.

Program Output:

Let’s compile and run the program using your favorite compiler. We are using the GCC compiler in this example.

Compile the program.

$ gcc function-return-pointer.c

Run the program.

Test Case 1:

function-returning-pointer-in-c-program-output

The user entered the desired array size as 10 and the program generated an array with ten random elements.

Let’s look at another example.

As expected the generateRandomArray() function generated an array with 5 random numbers, stored them in randArray and returned to main() function as a pointer variable.

Test Case 2:

Let’s look at the boundary checks (i.e Negative cases).

As we have defined the MAXSIZE of the array as 100, The program generates an error message if the number is out of bounds.

#define MAXSIZE 100

Please increase the above MAXSIZE variable to a desired value.

Related Programs:

Venkatesh

Hi Guys, I am Venkatesh. I am a programmer and an Open Source enthusiast. I write about programming and technology on this blog.

You may also like...

2 Responses

  1. […] Function Returning Pointer in C Programming Language […]

  2. […] size_t is an unsigned integer type. So The malloc() function allocates the size number of bytes and returns a pointer to the allocated block of memory. The returned pointer is a void pointer, so we need to typecast it […]

Leave a Reply