Reputation: 57
I have a struct that I want to statically allocate at compile time but I'm not sure how to return a pointer to the inner structure.
typedef struct
{
int d;
} innerStruct;
typedef struct
{
int a;
int b;
int c;
innerStruct inner;
} myStruct;
static myStruct m1;
innerStruct * getMyStructPtr()
{
myStruct * ptr = &m1;
return ptr->inner;
}
int main()
{
innerStruct * retval = getMyStructPtr();
return 0;
}
Link to online compiler: https://onlinegdb.com/SJAFJCy0Z
Upvotes: 0
Views: 44
Reputation: 134396
Check the data types!!
Your function promised to return a innerStruct *
, whereas your code attempts to returns a innerStruct
. They are neither same nor compatible. Fix either and use it appropriately.
Following the function call, it appears that you may want to write
return &(ptr->inner); // return type innerStruct *
Upvotes: 3