2016-12-22 1 views
0

これは私の最初の質問です! 私はリンクされたリストと機能を扱います。ポインタ、リンクリストと関数

構造体(Passenger)の値をリンクリストLIST1にコピーするこの関数を作成しました。

typedef struct 
    { 
    char fillname[40] 
    }PASSENGERS; 


typedef struct list1 
    { 
    char fullname[40]; 
    struct list1 *next; 
    }LIST1; 



    //COPYLIST 

copylist(LIST1 *list1, PASSENGERS *passenger) 
    { 

    LIST1 *start=NULL; 
    for (i=0;i<40;i++) 
     { 
     list1 = (LIST1 *) malloc (sizeof(LIST1)); 
     list1->next = NULL; 
     strcpy(list1->fullname,passenger[i].fullname); 

     if (start ==NULL) 
      start = list1; 
     else //add new node at the beginning of list 
      { 
      list1->next = start; 
      start = list1; 
      } 
     } 
    } 

インサイドメインでそれを印刷するとき、私は何を取得しかし、私は次の文

int main() 
PASSENGERS *passenger; 
int h; 

LIST1 *list1; 
list1=copylist(list1,passenger); 

で関数を呼び出す:私は機能や動きを使用しない場合

LIST1 *current = list1; 


    while (current !=NULL) 
     { 
     printf("%s",current->fullname); 
     current = current->next; 

メインの中のコードはうまく動作するので、おそらくそれはポインタの問題です、私はまだ使いこなそうとしています! あなたが

+1

あなたはCOPYLIST()関数から何も返されませんでした。 –

答えて

1

そのようなあなたのCOPYLIST機能を変更しますありがとう: -

LIST1 *copylist(LIST1 *list1, PASSENGERS *passenger) 
    { 

     LIST1 *start=NULL; 
     int i=0; 
     for (i=0;i<40;i++) 
      { 
      list1 = (LIST1 *) malloc (sizeof(LIST1)); 
      list1->next = NULL; 
      strcpy(list1->fullname,passenger[i].fullname); 

      if (start ==NULL) 
       start = list1; 
      else //add new node at the beginning of list 
       { 
       list1->next = start; 
       start = list1; 
       } 
      } 
      return start; 
    }