Skip to main content

Returning pointer from functions in C

Returning Pointers

  • In C, it is possible to return a pointer from a function.

  • This is useful when we want to allocate memory dynamically inside a function and then return a pointer to the allocated memory to the calling function.

  • The general syntax for returning a pointer from a function is as follows:

datatype *function_name(arguments) {
...
return pointer_variable;
}

Explanation

  • datatype is the type of the data that the pointer is pointing to (such as int, char, float, etc.)
  • function_name is the name of the function
  • arguments are the arguments passed to the function
  • pointer_variable is the variable that holds the memory address of the data to be returned.

When a function returns a pointer, the calling function can use this pointer to access the data that was allocated dynamically inside the called function.

note

It is important to note that when a function returns a pointer to dynamically allocated memory, it is the responsibility of the calling function to deallocate this memory using the free() function when it is no longer needed.

Example - Returning Pointers

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

int *allocate_memory(int size)
{
int *ptr = (int *)malloc(size * sizeof(int));
if (ptr == NULL)
{
printf("Failed to allocate memory.\n");
exit(1);
}
return ptr;
}

int main()
{
int n, i;
int *array;

printf("Enter the size of the array: ");
scanf("%d", &n);

array = allocate_memory(n);

for (i = 0; i < n; i++)
{
printf("Enter element %d: ", i + 1);
scanf("%d", &array[i]);
}

printf("The elements in the array are: ");
for (i = 0; i < n; i++)
{
printf("%d ", array[i]);
}

free(array);

return 0;
}
Output:

Explanation

  • The allocate_memory function is used to dynamically allocate memory for an integer array of a specified size.
  • The main function calls this function and assigns the returned pointer to the array variable.
  • The main function then reads in the elements of the array from the user and prints them out.
  • Finally, the main function deallocates the memory using the free function.

Other uses of returning pointer from functions

In addition to returning a pointer to an allocated block of memory, returning a pointer from a function in C can be useful in other situations:

Returning pointers to structures

A function can return a pointer to a structure that has been allocated dynamically, allowing the caller to access and modify the structure's data.

Returning pointers to static data

A function can return a pointer to static data that has been initialized in the function, allowing the caller to access the data.

Returning pointers to functions

A function can return a pointer to another function, allowing the caller to call that function at a later time.

Returning pointers to arrays

A function can return a pointer to an array that has been allocated dynamically or statically, allowing the caller to access and modify the array's elements.