-2

我想在c中获取指向链表中元素的指针。这是我的代码。我收到错误“返回类型'bigList'但预期'struct 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
     }
4

1 回答 1

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 有点随机。这很重要!

于 2013-02-28T21:39:21.933 回答