2013-02-28 94 views
-2

我想獲取指向c中鏈接列表中元素的指針。 這是我的代碼。我得到的錯誤「不兼容的類型時,返回類型'bigList',但'結構bigList **'的預期」。請幫忙。感謝找到指向鏈接列表中的元素的指針c

 /*this is the struct*/ 
    struct bigList 
    { 
     char data; 
     int count; 
     struct bigList *next; 
     }; 


     int main(void) 
     { 
     struct bigList *pointer = NULL; 

     *getPointer(&pointer, 'A'); //here how do I store the result to a pointer 

     } 

    /*function to return the pointer*/  
    bigList *getPointer(bigList *head, char value) 
    { 
     bigList temp; 
     temp=*head; 

     while(temp!=NULL) 
     { 
     if(temp->data==value) 
     break; 

     temp = temp->next;  
     } 
    return *temp;  //here I get the error I mentioned 
    } 
+1

至少語法,請...!如果你甚至懶得仔細閱讀C教程中關於指針語法的部分,那麼你對於發生什麼事情一定沒有絲毫的想法,這很危險。 – 2013-02-28 21:39:59

+0

對不起。即時通訊新的C,我忘了添加我使用的typedef聲明。我覺得我搞砸了...有很多東西要研究..指針混淆.. – user1476263 2013-02-28 21:58:43

回答

1

您需要2個三分球,頭指針到你的基地名單,並要返回指針的地方:

int main(void) 
    { 
    struct bigList *pointer = NULL; 

    struct bigList *retValPtr = getPointer(pointer, 'A'); //here how do I store the result to a pointer 

    } 

    struct bigList *getPointer(struct bigList *head, char value) 
    { 
     struct bigList *temp; // Don't really need this var as you could use "head" directly. 
     temp = head; 

     while(temp!=NULL) 
     { 
      if(temp->data==value) 
      break; 

      temp = temp->next;  
     } 

     return temp; // return the pointer to the correct element 
    } 

通知我如何圍繞指針使得它們改變所有相同的類型,而你的代碼是有點隨機的thsi。這很重要!

+0

謝謝,我忘了提及我使用的typedef decleration,看起來像我搞砸了.... – user1476263 2013-02-28 22:00:06