I statically defined a myStruct
and inside the struct, I have an innerStruct
array of size 3. I want to return a pointer to that array so I can assign a local pointer to point to one of the elements in order to modify the values later on.
At one part it seems like my compiler tells me I'm returning an int instead of innerStruct *
type.
How do I do it correctly? Is a double pointer necessary?
#include <stdio.h>
typedef struct
{
int content;
} innerStruct;
typedef struct
{
int a;
innerStruct inner[3];
} myStruct;
static myStruct m1;
innerStruct * getMyStructPtr()
{
return &(m1.inner);
}
int main()
{
int id = 0;
int elem = 1;
/* Define a local ptr so I can modify values of the m1 struct */
innerStruct * local_ptr = NULL;
innerStruct * myStruct_ptr = getMyStructPtr(); // seems like my compiler tells me I'm returning an int instead of innerStruct * type
/* Point to one of the elements */
local_ptr = &myStruct_ptr[elem];
local_ptr->content = 123;
return 0;
}
Link to online code compiler: https://onlinegdb.com/BJNO9lxA-