【发布时间】:2016-10-24 20:45:49
【问题描述】:
所以我正在尝试实现一个链表堆栈,它接受 char 参数并将它们添加到链接列表中,并将其 ascii 代码作为节点的值。
我将我的 nstack 指针传递给我的 push 函数并将它重新分配给 new_node 以创建一个新的顶部,但我的 push 函数似乎没有重新分配我的 nstack 节点 - 它只是打印最初初始化的 nstack价值。为什么不重新分配 nstack?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct list_node {
int element;
struct list_node * pnext;
};
void push(struct list_node *operators, int e);
int pop(struct list_node *operators);
int main(int argc, char *argv[]) {
int newvalue = (int)argv[1][0];
struct list_node * nstack = (struct list_node*)malloc(sizeof(struct list_node));
nstack->element = newvalue;
nstack->pnext = NULL;
int i;
for (i = 2; i < argc; i++) {
push(nstack, (int)argv[i][0]);
}
printf("top: %d\n", nstack->element);
}
void push(struct list_node *nstack, int e) {
struct list_node * new_node = (struct list_node*)malloc(sizeof(struct list_node));
new_node->pnext = nstack;
new_node->element = e;
nstack = new_node;
}
【问题讨论】:
-
C11 标准草案 n1570:6.5.2.2 函数调用 4 参数可以是任何完整对象类型的表达式。在准备调用函数时,会评估参数,并为每个参数分配相应参数的值。 93) 函数可以改变其参数的值,但这些改变不会影响参数的值。另一方面,可以传递一个指向对象的指针,函数可能会改变指向的对象的值。
-
void push(struct list_node *nstack, int e)可以是struct list_node *push(struct list_node *nstack, int e),它返回一个由调用者分配的指针。所以return new_node; -
短版:你的推送函数中的
nstack = new_node;对调用者来说毫无意义。你所改变的只是一个局部变量,而不是调用者的指针。要么利用函数的其他未使用结果来返回新的指针值,要么按地址传递调用者的指针(作为指向指针的指针)并通过取消引用对其进行修改。此问题的 许多 个重复项之一can be found here。
标签: c pointers linked-list stack