I am facing an error: request for member data in something not a structure or union. I am passing the address of a structure receiver_data in a structure global_vals to a function in another file.
The init_func function receives the address as a pointer. I used the arrow operator since its a pointer but I cant seem to access the structure. Both memory addresses &val.receiver_data and *ptr are the same so I'm not quite sure where went wrong
Would appreciate if someone can point out my mistake.
The components are split across various source/ header files with the following structure.
- main.c
- func_init.c
- datatypes.c
main.c
global_vals val;
void main(void)
{
val.receiver_data.data = 10;
init_func(&val.receiver_data);
}
func_init.c
void init_func(int *ptr_data)
{
printf("%d\n", ptr_data->data);
}
datatypes.h
typedef struct Receiver {
int data;
} Receiver;
typedef struct {
Receiver receiver_data;
// truncated
} global_vals;
The function parameter
has the type
int *that is it is a pointer to a scalar object. So this constructionptr_data->datais invalid because integers do not have data members likedata.It is not clear why the function is not declared like
An alternative approach is to declare and define the function like
and call it like
I hope that actually the function does something useful instead of just outputting an integer.