0

Why this error is being thrown for below code.

Error: error: expected ‘:’, ‘,’, ‘;’, ‘}’ or ‘__attribute__’ before ‘=’ token
  struct node_s *next=NULL;

Code:

#include<stdio.h>
#include<stdlib.h>

typedef struct node_s {
    int data;
    struct node_s *next=NULL;
} node;
P.P
  • 117,907
  • 20
  • 175
  • 238
Manu
  • 39
  • 1
  • 4

1 Answers1

1

You have to remove the NULL initialization of the pointer. An initialization is only allowed at the declaration.

typedef struct node_s {
     int data;
     struct node_s *next;
}node;

You can do node a = { .next = NULL }; to initialize the pointer during the declaration.

mch
  • 9,424
  • 2
  • 28
  • 42