2016-03-31 7 views
-3

私は"may be used uninitialized in this function" for: current->next = temp;を取得していますが、何時間も見ていますが解決策は見つかりませんでした。単一リンクリストc - この関数では初期化されていない(-Wall -Werror)

#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; 
} 
+3

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、あなたはあなたに機能を簡素化することができますか?そしてメッセージ全体を読む。それは実際には非常に明確です。 – Olaf

+0

私は現在の* tempの横に設定しています... if {}の中でcurren = tempを初期化する前にelse {}に決して置かれないので、 "なぜそれが非常に明確になるのですか? – TheLastStone

+0

「何時間も見ましたが、解決策を見つけることができませんでした」 - どうしてそうするのですか?あなたのデバッガは壊れていますか? –

答えて

0

head != NULLを想定、あなたがリストに最初の項目を追加した後、trueになります。

この

はコードです。あなたの関数は次に、:あなたは `current`を設定するのです

void add_list(int value){ 
    struct node *temp = malloc(sizeof(struct node)); 
    temp->data = value; 
    temp->next = head; 
    head = temp; 
} 
関連する問題