How to Pass or Return a Structure from a Function in C

Question: Can a Function in C Obtain Structures as Formal Arguments and Return Structures as Return Values?

Answer: Of course! A structure is a scalar variable like any other variable and therefore can be used wherever a variable is used. As we know that passing an argument by value in C requires copy of argument to be given to function. This requires entire structure to be copied to stack and discarded later. Let’s consider a simple program, for example,

/*copy_struct.c -- program creates copy of structure */
#include <stdio.h>
 
struct A {
          int a;
          char b[10];
    };
 
struct A copys(struct A);    /* function prototype */
 
int main(void)
{
    struct A x = {10, "hello"};
    struct A y;
 
    puts("Accessing y's members before calling to copys() ");
    printf("y.a is an integer: %d\n", y.a);
    printf("y.b is a string: %s\n", y.b);
    puts("");
 
    /* pass 'x' to copy fun. */
    y = copys(x);
    puts("Accessing y's members after calling copys() ");
    printf("y.a is an integer: %d\n", y.a);
    printf("y.b is a string: %s\n", y.b);
    puts("");
 
    return 0;
}
 
struct A copys(struct A temp)
{
    return temp;
}

Output as follows,

Accessing y's members before calling to copys()
y.a is an integer: 0
y.b is a string:
 
Accessing y's members after calling to copys()
y.a is an integer: 10
y.b is a string: hello

Notice that we called copys() and passed structure ‘x’ to it; the entire structure copied into function argument ‘temp’, then function without doing something useful on the copy of ‘x’ returned it to calling function. ‘temp’ structure is copied back into structure ‘y’ in the calling program. This way, we copied the entire structure twice. Structure ‘x’ occupies 16 bytes, so, we copied 32 bytes in two way. Of course, this approach gives correct result but this isn’t efficient. Worse it further, if only one member is to be manipulated, why we pass entire structure? Let’s try pointer technique. Unlike arrays, structure name isn’t an address. Instead, pass pointer to structure, for example,

advertisement
advertisement
    &x;    /* pointer to structure 'x' */

We here slightly modified structure declaration as,

struct A {
          int a;
          char *b;
    };

In order to copy structure ‘x’ to structure ‘y’, function call looks like,

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
    void copys(&x, &y);

and function definition becomes,

void copys(struct A const *x, struct A *y)
{
    y->a = x->a;
    y->b = x->b;
}

And output is as follows,

advertisement
Accessing y's members before calling to copys()
y.a is an integer: 0
y.b is a string: 1�I��^H��H���PTI���^F@
 
Accessing y's members after calling to copys()
y.a is an integer: 10
y.b is a string: hello

Notice here that we passed pointer to structure as argument to function, we copied 12
bytes here, then used indirection on pointers to access member’s of two structures and
copied structure ‘x’ directly to structure ‘y’. Now, we can access any or all members of structure. Further, because function worked on original data it didn’t return any value so it’s type is void. In order to prevent copys() from modifying the original contents of structure ‘x’, we used ‘const’ keyword in the function parameter.

So what you observe hare is that it’s always efficient to use pointer to access structures in function rather than passing structure by value. The bigger the structure, more efficient pointer technique is!

advertisement

The only draw back associated with pointers is that they access original contents of structure directly. There are chances pointer modifies the original content. To prevent this, use ‘const’ keyword!

Sanfoundry Global Education & Learning Series – 1000 C Tutorials.

If you wish to look at all C Tutorials, go to C Tutorials.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.