I know that in C we cannot return an array from a function, but a pointer to an array. But I want to know what is the special thing about structs
that makes them return-able by functions even though they may contain arrays.
Why is the struct
wrapping makes the following program valid?
#include <stdio.h>
struct data {
char buf[256];
};
struct data Foo(const char *buf);
int main(void)
{
struct data obj;
obj = Foo("This is a sentence.");
printf("%s\n", obj.buf);
return 0;
}
struct data Foo(const char *buf)
{
struct data X;
strcpy(X.buf, buf);
return X;
}