c - 初始化链表时获取 "missing braces around initializer"

标签 c debugging gcc warnings

我正在实现一个链表:它具有简单的结构和两个辅助函数,附加到 llist 的末尾并打印 llist。

    typedef struct target{
      char my_target[65];
      int ID;
      struct target *next;
    }target_list;

    void append(target_list *head, char* new_data, int new_int){
  target_list *current = head;
  while (current -> next != NULL){
    current = current -> next;
  }
  current -> next = malloc(sizeof(target_list));
  strcpy(current -> next -> my_target,new_data);
  current -> next -> next = NULL;
  current -> next -> ID = new_int;
}

void print_list(target_list* head){
  target_list* current = head;
  while(current != NULL){
    printf("%d\n",current -> ID);
    printf("%s\n",current -> my_target);
    current = current -> next;
  }
}
    target_list head = {NULL}; 
     while(fgets (str , 1024 , f) > 0){
        char* num_args;
        char my_target[65];
        target_list head = {NULL};   //ERROR!
        int target_id = 0;
        int index_1,index_2;

        num_args = strtok(str," ");
        while (num_args != NULL)
        {
          if (num_args[0] != '\n'){
          append(&head,num_args,target_id);
          printf("the node now is %d\n",target_id);
          target_id++;
        }
          num_args = strtok(NULL," ");
        }
        print_list(&head);
        }

我收到如下警告消息:

original.c:283:5: warning: missing braces around initializer [-Wmissing-braces]
     target_list head = {NULL}; 
     ^
original.c:283:5: warning: (near initialization for 'head.my_target') [-Wmissing-braces]
original.c:283:5: warning: initialization makes integer from pointer without a cast [enabled by default]
original.c:283:5: warning: (near initialization for 'head.my_target[0]') [enabled by default]

有什么想法吗?我以前实现过 llist,但从未见过像这样的消息。

最佳答案

在语句target_list head中,head普通结构变量而不是指针变量,无法初始化其中 NULL(void*)0

有一些事情你应该注意,我只是过滤掉问题并列出来。

  • head 应该是 struct 指针 类型,而不是普通的结构变量。
    target_list head = {NULL} 应为 target_list *head = NULL;
  • 第二次不需要再次声明head,例如target_list head = {NULL};,它应该只是head = NULL;<
  • 像这样调用 append() append(head,num_args,target_id);print_list(head);

正确检查append()功能。

关于c - 初始化链表时获取 "missing braces around initializer",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49838922/

相关文章:

objective-c - 在哪里可以找到 kCG KeyboardEvent Keycode 西里尔语言键码列表

c - 使用回溯递归的自回避行走

debugging - 使用 Xcode 4 在 iphone4 设备中调试时如何指定 arm6?

java - 不幸的是,MyApp 已停止。我该如何解决这个问题?

c - sbss2/sdata2有什么意义?

c - gcc -E 不扩展 C11 _Generic 表达式

c - flock(),然后是 fgets() : low-level locks,,然后是 stdio 读/写库函数。是否可以?

c - C中的显示缓冲区

ios - _UIRemoteView - 在 Xcode 中显示 View 层次结构

c++ - 如何摆脱内部编译器错误 : Illegal instruction min() _GLIBCXX_USE_NOEXCEPT { return __FLT_MIN__; }