Wednesday, August 29, 2018

Return a `struct` from a function in C



Today I was teaching a couple of friends how to use C structs. One of them asked if you could return a struct from a function, to which I replied: "No! You'd return pointers to dynamically malloced structs instead."




Coming from someone who primarily does C++, I was expecting not be able to return structs 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?



[edit]: Ok, I was just pointed struct b = a is a syntax error -- that's correct and I'm an idiot! But that makes it even more complicated! Using struct MyObj b = a does indeed work! What am I missing here?



Answer



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 

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 


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;
}

No comments:

Post a Comment

plot explanation - Why did Peaches' mom hang on the tree? - Movies & TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...