Home > OS >  What causes "member reference base type 't_list *' is not a structure or union"?
What causes "member reference base type 't_list *' is not a structure or union"?

Time:07-14

I'm working with linked lists and I have the following struct:

typedef struct s_list
{
    void            *content;
    struct s_list   *next;
}   t_list;

I was trying to make a function that swapped first 2 elements and tried this:

void sa(t_list  **a)
{
    if (!*a && !*a->next)
        return ;
}

And it gave me the following two errors:

member reference base type 't_list *' (aka 'struct s_list *') is not a structure or union gcc
expression must have pointer-to-struct-or-union type but it has type "t_list **" C/C  (132)

However if I do this:

void sa(t_list  **a)
{
    t_list  *t;

    t = *a;
    if (!*a && !t->next)
        return ;
}

It works. Why is this? What am I missing?

CodePudding user response:

Operator precedence. -> has higher operator precedence than unary *. Hence "expression must have pointer-to-struct-or-union type but it has type "t_list **", because the compiler tries to apply ->next on a t_list** type.

Fix: (*a)->next.

  • Related