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

斯特拉

我的构建失败了。我不确定为什么...

简介:对于此作业,您必须编写 ac 程序,该程序将采用中缀表达式作为输入并显示输入的后缀表达式。转换为后缀表达式后,程序应从后缀计算表达式并显示结果。

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

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)* capacity);

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章