2013-02-28 103 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