【问题标题】:finding the pointer to an element in linked list in c在c中找到指向链表中元素的指针
【发布时间】:2013-02-28 21:35:59
【问题描述】:

我想在 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
     }

【问题讨论】:

  • 请至少语法...!如果您甚至没有仔细阅读该 C 教程中有关指针语法的部分,那么您肯定对发生的事情一无所知,这很危险。
  • 对不起。我是 c 新手,我忘了添加我使用的 typedef 声明。我想我搞砸了......有很多东西要研究......指针令人困惑......

标签: c struct linked-list


【解决方案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
   }

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

【讨论】:

  • 谢谢,我忘了提到我使用的 typedef decleration,看来我搞砸了......
猜你喜欢
  • 2018-07-28
  • 1970-01-01
  • 2013-08-07
  • 1970-01-01
  • 2010-10-13
  • 2012-08-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多