gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 20:27:58 28 4
gpt4 key购买 nike

我想获取指向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
}

最佳答案

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

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

关于c - 在c中查找指向链表中元素的指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15146109/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com