gpt4 book ai didi

c 指针作为输入

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

当我尝试将元素插入堆栈时出现段错误,但如果我打开堆栈地址(我用“!!!”标记它们)及其符号,它会接受它。但是这次在每次推送中,它都会创建新地址并且不会增加最高值。

typedef struct
{
struct table **symbols; // array of the stack
int top; //index of the top element
int size; //maximum size of the stack
}stack;

void push(stack *stck,struct table *element)
{
if(stck->top == stck->size)
{
printf("stack is full");
return;
}

stck = malloc(sizeof(stack)); !!!
stck->symbols = (struct table **)malloc(50 * sizeof(struct table*)); !!!

printf("top : %d\n",stck->top);
stck->top = stck->top++;
printf("%d"&stck->top);
stck->symbols[stck->top] = element;
printf("top : %d\n",stck->top);
}

最佳答案

你必须先构建你的堆栈,然后才能将任何东西压入它。例如。创建函数 stack_new 将为您的堆栈分配内存并初始化其成员:

stack * stack_new (size_t size)
{
stack * stck = malloc(sizeof(stack));
stck->top = -1;
stck->size = size
stck->symbols = (struct table **)malloc(size * sizeof(struct table*));
return stck;
}

现在,一旦您使用上述函数正确构造了堆栈,就可以将其传递给 push 函数。

关于c 指针作为输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1941243/

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