我得到"may be used uninitialized in this function" for: current->next = temp;
我查了好几个小时但找不到任何解决方案。
这是代码:
#include <stdio.h>
#include <stdlib.h>
struct node{
int data;
struct node *next;
}*head = NULL;
void add_list( int value){
struct node *temp, *current;
temp = (struct node *) malloc(sizeof(struct node));
temp->data = value;
temp->next = NULL;
if(head == NULL){
head = temp;
current = temp;
}
else{
current->next = temp;
current = temp;
}
}
int main(void){
for(int i = 0; i < 10; i++){
add_list(i);
}
return EXIT_SUCCESS;
}
答案 0 :(得分:0)
假设head != NULL
,将第一个项目添加到列表后将为true。然后,你的功能是:
void add_list( int value){
struct node *temp, *current;
temp = (struct node *) malloc(sizeof(struct node));
temp->data = value;
temp->next = NULL;
current->next = temp; // current is uninitialized
current = temp;
}
FWIW,您可以将功能简化为:
void add_list( int value){
struct node *temp = malloc(sizeof(struct node));
temp->data = value;
temp->next = head;
head = temp;
}