Function returning address

Home / C Programming / Function returning address

Function returning address


In C programming, a function can return a pointer that holds the address of a memory location. This allows you to dynamically allocate memory inside the function and return the address of the allocated memory to the calling function. Returning addresses from functions is commonly used when you want to create and manipulate data structures dynamically or when you need to return arrays or strings.

Here's an example of a function that dynamically allocates memory for an integer array and returns the address of that array:


In this example, the function `createArray` takes the size of the array as an argument and dynamically allocates memory for an integer array of that size using the `malloc` function. It then returns the address of the first element of the allocated array. In the `main` function, we call `createArray` and receive the address of the dynamically allocated array in the `myArray` pointer.

After using the dynamically allocated memory, it's essential to free it using the `free` function to avoid memory leaks.