As @laserlight stated you could pass the struct as a parameter, but sometimes I had a habit to not do so. I don't know maybe because Its not exotic.
You can use malloc:
Code:
#include <stdio.h>
#include <stdlib.h>
struct x
{
int member;
};
struct x* foo ()
{
struct x* internalStruct = malloc(sizeof(struct x));
internalStruct->member = 1;
return internalStruct;
}
int main()
{
struct x* ptr = foo();
printf("%i", ptr->member);
return 0;
}
Or you can use your struct as an array of members:
Code:
#include <stdio.h>
#include <stdlib.h>
struct x
{
int member;
};
struct x* foo ()
{
static struct x internalStruct[] = {1};
return internalStruct;
}
int main()
{
struct x* ptr = foo();
printf("%i", ptr->member);
return 0;
}
Same thing. Array decays to a pointer to the first element. Structure is the same thing alrady. Thats one of the reasons why they are considered very similar.