2014-04-18 27 views
1

我想在c中寫一個遞歸函數來打印一個鏈表。這是我的鏈表結構:在c中,使用if語句的結構項

typedef struct list_node{ 
     char in_data; 
     struct list_node *next_item; 
     } list_node; 

這裏是我的打印列表功能:

void print_list(list_node *head_of_list){ 
     list_node *new_ptr = head_of_list.next_item; 
     if(new_ptr == '\0'){ 
       printf(" %c", head_of_list->in_data); 
     } 
     else{ 
       printf(" %c", head_of_list->in_data); 
       print_list(head_of_list->next_item); 
     } 
     return; 
} 

這裏是我不斷收到錯誤:

error: request for member ‘next_item’ in something not a structure or union 

和錯誤說,這是對的與if(new_ptr ...語句一致。任何人都可以幫我消除這個錯誤嗎?謝謝

回答

3

list_node *new_ptr = head_of_list.next_item;
必須
list_node *new_ptr = head_of_list->next_item;

+0

謝謝!這解決了它! – jani

0

head_of_list不是list_node,這是一個list_node*,這是一個指向list_node。要訪問其成員,你應該寫:

head_of_list->next_item;