Search code examples
cpointersstructdesignated-initializer

How to change pointer variables within a struct?


So I am trying to implement a linked list and I have this struct for each of the nodes:

    typedef struct listNode {
        struct listNode *next;
        void *data;
    } NODE;

Previously, I had made a game where the spacecraft struct looked like this:

   typedef struct {
       int height;
       int width;
   } SPACECRAFT;

and I could make a new spacecraft by doing

  SPACECRAFT plyrShip = {
      .width = //someInt;
      .height = //someInt;
  };

Though with the nodes, the variables are pointers and it isn't allowing me to create a new node by doing

  NODE newNode = {
      .*next = null;
      .*data = *data //function has *data as parameter so I can pass it 
                     //into the node
  }

What am I doing wrong?


Solution

  • You have conflated the pointer declaration syntax and the designated initializer syntax. A member field may be declared to be a pointer type. When using designated initializers, just name the field. Also note they are comma separated.

      NODE newNode = {
          .next = null,
          .data = data
      };