C 传递给函数的指针未重新分配

C 传递给函数的指针未重新分配,c,pointers,linked-list,stack,C,Pointers,Linked List,Stack,因此,我尝试实现一个链表堆栈,它接受char参数,并将它们添加到链接列表中,使用ascii码作为节点的值 我将nstack指针传入push函数,并将其重新分配给新的_节点,以便创建新的top,但我的push函数似乎没有重新分配nstack节点-它只是打印最初初始化的nstack值。为什么不重新分配nstack #include <stdio.h> #include <stdlib.h> #include <string.h> struct list_node

因此,我尝试实现一个链表堆栈,它接受char参数,并将它们添加到链接列表中,使用ascii码作为节点的值

我将nstack指针传入push函数,并将其重新分配给新的_节点,以便创建新的top,但我的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;
}
#包括
#包括
#包括
结构列表节点{
int元素;
结构列表\u节点*pnext;
};
void push(结构列表节点*操作符,int e);
int-pop(结构列表\节点*运算符);
int main(int argc,char*argv[]){
int newvalue=(int)argv[1][0];
结构列表节点*nstack=(结构列表节点*)malloc(sizeof(结构列表节点));
nstack->element=newvalue;
nstack->pnext=NULL;
int i;
对于(i=2;ielement);
}
无效推送(结构列表节点*nstack,int e){
结构列表节点*新节点=(结构列表节点*)malloc(sizeof(结构列表节点));
新建节点->pnext=nstack;
新建_节点->元素=e;
nstack=新节点;
}

因为您正在传递指针的副本(按值)。您需要这样的内容(指针到指针):

#包括
#包括
#包括
结构列表节点{
int元素;
结构列表\u节点*pnext;
};
void push(结构列表\节点**操作符,int e);
int-pop(结构列表\节点*运算符);
int main(int argc,char*argv[]){
//int newvalue=(int)argv[1][0];
int newvalue=1;
结构列表节点*nstack=(结构列表节点*)malloc(sizeof(结构列表节点));
nstack->element=newvalue;
nstack->pnext=NULL;
int i;
对于(i=2;i<7;i++){
//推送(nstack,(int)argv[i][0]);
推送(&nstack,i);
}
printf(“顶部:%d\n”,nstack->element);
}
无效推送(结构列表节点**nstack,int e){
结构列表节点*新节点=(结构列表节点*)malloc(sizeof(结构列表节点));
新建节点->pnext=*nstack;
新建_节点->元素=e;
*nstack=新节点;
}

展开塔罗德的正确点。调用方函数所做的是获取指针值的副本,并将其放入堆栈或寄存器(依赖于编译器)中,然后在推送函数中使用。但是,在push函数中,您实际上是在返回之前更改此寄存器或堆栈位置中的值。但是,当您返回时,调用函数基本上会丢弃这些信息(它从堆栈中弹出,或者根据编译器的不同,将寄存器用于其他内容)。解决这个问题的唯一方法是传递指针的地址,并在需要写入指针时遵循它,如Tarod所示。

C11标准草案n1570:6.5.2.2函数调用4参数可以是任何完整对象类型的表达式。在准备调用函数时,将计算参数,并为每个参数指定相应参数的值。93)函数可以更改其参数的值,但这些更改不会影响参数的值。另一方面,可以传递指向对象的指针,该函数可以更改指向的对象的值。
void push(struct list\u node*nstack,int e)
可以是
struct list\u node*push(struct list\u node*nstack,int e)
,返回调用者分配的指针。因此
返回新的_节点短版本:
nstack=new_节点对调用者来说毫无意义。您所要更改的只是一个局部变量,而不是调用方的指针。或者利用函数的未使用结果返回新指针值,或者按地址传递调用方的指针(作为指向指针的指针),并通过反引用修改它。这个问题的众多重复之一。谢谢。我感谢你的评论。我投你一票:)
#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];
    int newvalue = 1;
    struct list_node * nstack = (struct list_node*)malloc(sizeof(struct list_node));

    nstack->element = newvalue;
    nstack->pnext = NULL;

    int i;
        for (i = 2; i < 7; i++) {
            //push(nstack, (int)argv[i][0]);
            push(&nstack, i);
        }
    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;
}