Return a `struct` from a function in C
c
Solution
You can return a structure from a function (or use the `=` operator) without any problems. It's a well-defined part of the language. The only problem with `struct b = a` is that you didn't provide a complete type. `struct MyObj b = a` will work just fine. You can pass structures to functions as well - a structure is exactly the same as any built-in type for purposes of parameter passing, return values, and assignment.
Here's a simple demonstration program that does all three - passes a structure as a parameter, returns a structure from a function, and uses structures in assignment statements:
#include <stdio.h>
struct a {
int i;
};
struct a f(struct a x)
{
struct a r = x;
return r;
}
int main(void)
{
struct a x = { 12 };
struct a y = f(x);
printf("%d\n", y.i);
return 0;
}
The next example is pretty much exactly the same, but uses the built-in `int` type for demonstration purposes. The two programs have the same behaviour with respect to pass-by-value for parameter passing, assignment, etc.:
#include <stdio.h>
int f(int x)
{
int r = x;
return r;
}
int main(void)
{
int x = 12;
int y = f(x);
printf("%d\n", y);
return 0;
}
Problem
Today I was teaching a couple of friends how to use C `struct`s. One of them asked if you could return a `struct` from a function, to which I replied: "No! You'd return pointers to dynamically `malloc`ed `struct`s instead." Coming from someone who primarily does C++, I was expecting not be able to return `struct`s by values. In C++ you can overload the `operator =` for your objects and makes complete sense to have a function to return your object by value. In C, however, you do not have that option and so it got me thinking what the compiler is actually doing. Consider the following: ``` struct MyObj{ double x, y; }; struct MyObj foo(){ struct MyObj a; a.x = 10; a.y = 10; return a; } int main () { struct MyObj a; a = foo(); // This DOES work struct b = a; // This does not work return 0; } ``` I understand why `struct b = a;` should not work -- you cannot overload `operator =` for your data type. How is it that `a = foo();` compiles fine? Does it mean something other than `struct b = a;`? Maybe the question to ask is: What exactly does the `return` statement in conjunction to `=` sign do?