gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 19:05:26 29 4
gpt4 key购买 nike

我正在实现一个链表:它具有简单的结构和两个辅助函数,附加到 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/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com