C Programming Tutorial

C Programming Tutorial

Return Pointer from Function in C

Hello Everyone, In this article, we will cover the concept of Return Pointer from Function in C Programming. We will cover the basics, provide examples, and explain how to safely handle pointers to avoid common pitfalls. Whether you’re new to C or have some experience, this guide will help you to understand concet of Return Pointer from Function in C effectively. Let’s get started.

In C programming, a return pointer from a function refers to returning the address of a memory location, typically of an array or a structure, back to the calling function. This is useful when you need to return more than one value or a large amount of data from a function.

To demonstrate this concept, let’s consider an example where we create a function that takes two integers as input and returns a pointer to an array containing the sum and product of the two input integers:

#include <stdio.h>// Function prototype
int* sum_and_product(int a, int b);

int main() {
    int a = 5, b = 3;
    int *result;

    // Calling the function and storing the returned pointer
    result = sum_and_product(a, b);

    printf("Sum: %d\\n", result[0]);
    printf("Product: %d\\n", result[1]);

    return 0;
}

// Function definition
int* sum_and_product(int a, int b) {
    static int output[2];

    output[0] = a + b;
    output[1] = a * b;

    // Returning the address of the output array
    return output;
}

Output:

Sum: 8
Product: 15

In this example, we have a function sum_and_product() that takes two integers a and b as input and returns a pointer to an integer. Inside the function, we create a static integer array output of size 2. We store the sum and product of the input integers in the first and second elements of the array, respectively. Since the array is static, it will not be destroyed when the function returns, and its address can be safely returned to the calling function.

In the main() function, we call sum_and_product() and store the returned pointer in result. We then use this pointer to access the sum and product in the output array by indexing the pointer with [0] and [1], respectively. Finally, we print the sum and product.

Continuing with the previous example, we can also demonstrate how to return a pointer to a structure from a function. Let’s consider a case where we define a structure Point with two integer members x and y. We will create a function that takes two integers as input and returns a pointer to a Point structure containing the input integers as its coordinates.

Here’s the c program for this example:

#include <stdio.h>
#include <stdlib.h>// Structure definition
typedef struct {
    int x;
    int y;
} Point;

// Function prototype
Point* create_point(int x, int y);

int main() {
    int x = 5, y = 3;
    Point *point;

    // Calling the function and storing the returned pointer
    point = create_point(x, y);

    printf("Point coordinates: (%d, %d)\\n", point->x, point->y);

    // Free the memory allocated to the point
    free(point);

    return 0;
}

// Function definition
Point* create_point(int x, int y) {
    Point *new_point;

    // Allocate memory for the new point
    new_point = (Point*) malloc(sizeof(Point));

    // Check if memory allocation was successful
    if (new_point == NULL) {
        printf("Memory allocation failed.\\n");
        exit(1);
    }

    // Assign the coordinates
    new_point->x = x;
    new_point->y = y;

    // Return the address of the new point
    return new_point;
}

Output:

Point coordinates: (5, 3)

In this example, we define a Point structure with two integer members x and y. We create a function create_point() that takes two integers x and y as input and returns a pointer to a Point structure.

Inside the create_point() function, we allocate memory for a new Point structure using malloc(). We then check if memory allocation was successful, and if not, we print an error message and exit the program. We assign the input coordinates to the new point and return its address.

In the main() function, we call create_point() and store the returned pointer in point. We then use the pointer to access the coordinates in the Point structure using the -> operator. Finally, we print the coordinates and free the memory allocated to the point using free().

Share This Article