0

Im trying to update a field of a struct that's a pointer like so:

typedef struct Data *STRUCT_POINTER;
typedef struct Data
{
    int element;
    STRUCT_POINTER next;
} STRUCT_NODE;

 void manipulate(STRUCT_POINTER *data ) { 
        data->element = 1;
}

But I get a warning saying

expression must have pointer-to-struct-or-union type but it has type "STRUCT_POINTER *"

My question is, how how do I update the field element from the Data struct while using it as a pointer? Or is it even possible?

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
Cloyd Abad
  • 617
  • 8
  • 16

1 Answers1

2

The function parameter

void manipulate(STRUCT_POINTER *data ) { 
        data->element = 1;
}

has the type struct Data **. So dereferencing the pointer you again get a pointer of the type struct Data *.

You need to declare the function like

void manipulate(STRUCT_POINTER data ) { 
        data->element = 1;
        data->next = NULL;
}
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • 3
    Better yet, eliminate the pointer typedef and just use `STRUCT_NODE *`. The bug was a direct result of hiding pointers in typedefs. – Tom Karzes Oct 30 '22 at 15:36