gpt4 book ai didi

c - 构建的正确返回类型是什么?

转载 作者:行者123 更新时间:2023-11-30 16:12:47 25 4
gpt4 key购买 nike

我的构建失败。我不知道为什么...

Introduction: For this assignment you have to write a c program that will take an infix expression as input and display the postfix expression of the input. After converting to the postfix expression, the program should evaluate the expression from the postfix and display the result.

我们必须为此使用堆栈,但我已经无法构建第一个动态分配的堆栈。

typedef struct stack
{
int top;
int capacity;
int *arr;

} stack;

stack* createStack(int capacity) //create a stack, must allocate a box of memory of pointer to that stack
{
stack* s = malloc(sizeof(stack)*1);
s->top = -1; //bc it's a pointer, use an arrow or (*s).top;
s-> capacity = capacity; //one's a pointer, one's what's
//passed to it
s-> arr = malloc(sizeof(char)* capacity);
//defensive coding: checks to see if we found space for the
//array
if(s->arr == NULL)
{
printf("Failed to find space for the array.\n");
free(s);
return NULL;
}
else
return s;
}

最佳答案

您有整数指针int *arr;,但在您的代码中分配的内存较少s-> arr = malloc(sizeof(char)*容量);

相反,你应该写:s-> arr = malloc(sizeof(int)* 容量);

关于c - 构建的正确返回类型是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58256188/

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