在c语言中查找指向链表元素的指针



我想获得c中链表元素的指针。这是我的代码。我得到错误"当返回类型' bigList '时不兼容的类型,但'结构bigList ** '是预期的"。请帮助。由于

     /*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
     }

你需要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
   }

注意我是如何改变指针的,这样它们都是相同的类型,而你的代码是随机的。这很重要!

相关内容

  • 没有找到相关文章

最新更新