Return a Struct From Function in C

  1. Use Standard Notation to Return struct From Function
  2. Use Pointer Notation to Return struct From Function

This article will demonstrate multiple methods about how to return a struct from a function in C.

Use Standard Notation to Return struct From Function

The struct keyword in C is used to implement user-defined data structures. Since we define the struct type in this example, it will be a more clean notation for function declarations if we typedef the MyStruct structure. It will associate a new type alias for the given structure, and we’d need to specify only the new alias name in the function prototype. Now, functions in C can return the struct similar to the built-in data types.

In the following example code, we implemented a clearMyStruct function that takes a pointer to the MyStruct object and returns the same object by value. Note that we need to access struct members using the -> operator when the handle is the pointer to the struct.

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

enum {DAY = 9, MONTH = 2};

typedef struct {
    int day;
    int month;
} MyStruct;

MyStruct clearMyStruct(MyStruct *st)
{
    st->day = 0;
    st->month = 0;

    return *st;
}

int setMyStruct(MyStruct *st, int d, int m)
{
    if (!st)
        return -1;
    st->day = d;
    st->month = m;
    return 0;
}

int main() {
    MyStruct tmp;

    if (setMyStruct(&tmp, DAY, MONTH) == -1 )
        exit(EXIT_FAILURE);

    printf("day: %d\nmonth: %d\n", tmp.day, tmp.month);
    clearMyStruct(&tmp);
    printf("day: %d\nmonth: %d\n", tmp.day, tmp.month);

    exit(EXIT_SUCCESS);
}

Output:

day: 9
month: 2
day: 0
month: 0

Use Pointer Notation to Return struct From Function

Generally, struct defined data structures tend to contain multiple data members resulting in a big memory footprint. Now, when it comes to passing relatively big structures between functions, it’s best to use pointers. The pointer serves as a handle to the object and its size is fixed regardless of the structure stored there. Using pointers to return struct potentially reduces memory traffic and gives code more performance. Although the program is compiled with optimization flags, it may implicitly modify the data passing instructions. Note that we utilized the enum type to declare named constant integer values.

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

enum {DAY = 9, MONTH = 2};

typedef struct {
    int day;
    int month;
} MyStruct;

MyStruct *clearMyStruct2(MyStruct *st)
{
    st->day = 0;
    st->month = 0;

    return st;
}

int setMyStruct(MyStruct *st, int d, int m)
{
    if (!st)
        return -1;
    st->day = d;
    st->month = m;
    return 0;
}

int main() {
    MyStruct tmp;

    if (setMyStruct(&tmp, DAY, MONTH) == -1 )
        exit(EXIT_FAILURE);

    printf("day: %d\nmonth: %d\n", tmp.day, tmp.month);
    clearMyStruct2(&tmp);
    printf("day: %d\nmonth: %d\n", tmp.day, tmp.month);

    exit(EXIT_SUCCESS);
}

Output:

day: 9
month: 2
day: 0
month: 0
Contribute
DelftStack is a collective effort contributed by software geeks like you. If you like the article and would like to contribute to DelftStack by writing paid articles, you can check the write for us page.

Related Article - C Struct

  • Initialize a Struct in C