gpt4 book ai didi

c - 指针没有取新值

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

对于以下函数:

/*this function removes the topmost item in the stack*/
void pop(Stack * S, NODE * returnNode) {
stackNode * temp;

if(S->root == NULL) {
ERROR("Sorry, cannot pop an empty stack!!\n");
}
else {
temp = S->root;
returnNode = temp->Item;/*x now points to the topmost node*/
S->root = temp->nextItem;/*stack points to the rest of the list*/
free(temp);/*returning memory to the system*/
}
}

我希望 returnNode 指针具有与 temp->Item 相同的值,但是当我检查 GDB 中的值时它没有。我错过了什么吗?

我应该补充一点,temp 值设置正确。

最佳答案

如果你想更新一个指针作为参数,你需要传递它的地址。否则,您只是在更新调用堆栈上的值,该值在范围内是本地的。

void pop(Stack * S, NODE ** returnNode) {
stackNode * temp;

if(S->root == NULL) {
ERROR("Sorry, cannot pop an empty stack!!\n");
}
else {
temp = S->root;
*returnNode = temp->Item;/*x now points to the topmost node*/
S->root = temp->nextItem;/*stack points to the rest of the list*/
free(temp);/*returning memory to the system*/
}
}

关于c - 指针没有取新值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28387158/

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