在结构定义中分配下一个指针 = NULL,抛出错误

Assigning next pointer in a structure definition = NULL, throws error

为什么下面的代码会抛出这个错误。

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

代码:

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

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

您必须删除指针的 NULL 初始化。只允许在声明处进行初始化。

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

您可以node a = { .next = NULL };在声明时初始化指针。