I understand that this should not be done, but I believe that I have seen examples that do something like this (note code is not necessarily syntactically correct, but there is an idea)
typedef struct{ int a,b; }mystruct;
And then here is the function
mystruct func(int c, int d){ mystruct retval; retval.a = c; retval.b = d; return retval; }
I realized that we should always return a pointer to the malloc'ed structure if we want to do something like this, but I'm sure I saw examples that do something like this. It's right? Personally, I always either return a pointer to the malloc'ed structure, or simply pass a reference to the function and change the values ββthere. (Since I understand that once the scope of the function is finished, any stack used to allocate the structure can be overwritten).
Add the second part of the question: does it depend on the compiler? If so, what is the behavior of the latest desktop compilers: gcc, g ++, and Visual Studio?
Thoughts on this?
c ++ c function struct return-type
jzepeda Mar 06 2018-12-12T00: 00Z
source share