gpt4 book ai didi

c - 返回一个指针并修改该指针指向的内容

转载 作者:太空宇宙 更新时间:2023-11-04 04:47:32 25 4
gpt4 key购买 nike

我在一个函数中有一个指针,我想返回该指针,以便我以后可以修改它指向的内容。返回它会返回指针指向的地址还是指针本身?这个问题是因为我想更改链接列表的头部指向的内容。

例如

struct node_{
//variables
}*headPtr=NULL; //assume when we are returning headPtr in foo() it is no longer NULL but points to something

typdef struct node_ node;

node foo(){
//some if conditions
return headPtr;
}

main(){
node *tmpPtr;
tmpPtr=foo();
}

最佳答案

此函数采用指向链表头部的指针和一些其他参数,创建一个新节点并将列表附加到它并返回新的头部。

 struct type *ptr_change_head(struct type *ptr_old_head, other args){
struct type *ptr = ptr_old_head;
struct type *ptr_new_head = <new head>;

ptr_new_head->ptr_next = ptr;

return ptr_new_head;

}

关于c - 返回一个指针并修改该指针指向的内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19471465/

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