In the codebase I'm working on, I have two structures defined like so:
typedef struct s1
{
int somedata;
union su
{
char *cptr;
struct s1 *array;
}; //line 123
} S1;
typedef struct s2
{
S1 data;
} S2;
Elsewhere, these structures and members are accessed in a way that confuses me:
S2 *s2_ptr;
s2_ptr->data.array[1].charptr
So I have questions:
Why is it array[1].charptr
and not array[1]->charptr
since array
is a pointer?
When compiled, I get the warning "declaration does not declare anything" on "line 123" in struct s1
above. I've read other questions about this and it seems I need to make the union declaration something like:
union su
{
...
} SU;
which then causes compiler errors for obvious reasons. Of course then correcting the calls to s2_ptr->data.SU.array[1].charptr
fixes the errors. I'm wondering if that Would in any way alter what's happening in how data is accessed? Would anything change?
Thanks for any help.
*Thanks much for all the answers. Cleared me right up.