简体   繁体   English

当我尝试使用 push 和 pop function 时,为什么会出现错误?

[英]Why am I getting an error when I try to use push and pop function?

The question asks us to "Write the program by completing the main function that calls the push function at least three times , then prints out the updated stack, then calls the pop function and prints out the updated stack again."该问题要求我们“编写程序,完成调用 push function 至少三次的 main function,然后打印出更新的堆栈,然后调用 pop function 并再次打印出更新的堆栈。”

The code tells me that the compilation failed due to the following reasons: Line 10 |代码告诉我编译失败,原因如下: Line 10 | { Which to me does not make sense. { 这对我来说没有意义。 I tried removing it but it gives other errors我尝试删除它,但它给出了其他错误

Additionally, the code gives a warning saying " warning: array 'stack' assumed to have one element" Which I have no idea what that means.此外,代码给出了一条警告,说“警告:数组‘堆栈’假设有一个元素”,我不知道那是什么意思。

This is the code:这是代码:

#include <stdio.h>
#define STACK_EMPTY '0'
#define STACK_SIZE 20

char stack[], item;
int *top, max_size;

void
push(char stack[], char item, int *top, int  max_size),
{
     if (*top < max_size-1) 
     {
         --(*top);
         stack[*top] = item;
     }
}

char
pop (char stack[],    /* input/output - the stack */
    int *top)        /* input/output - pointer to top of stack */
{
    char item;       /* value popped off the stack */

    if (*top >= 0) 
    {
        item = stack[*top];
        --(*top);
    } 
    else 
    {
         item = STACK_EMPTY;
    }

    return (item);
}

int
main (void)
{
   char s [STACK_SIZE];
   int s_top = -1; // stack is empty

   if (*top <= -1)
{
    item = STACK_EMPTY;
}
   return (0);
}

Issue is in how you are handling the top pointer.问题在于您如何处理顶部指针。 you decrement the pointer ie, --top, NOT the value pointed by it.您递减指针,即--top,而不是它指向的值。 Also push should increment it ie, ++top.推也应该增加它,即 ++top。

---Here is the corrected code ---- ---这是更正后的代码----


#include <stdio.h>
#define STACK_SIZE 20
#define STACK_EMPTY '0'

char item;
int top_idx = 0;

void
push(char *stack, char item)
{
     if (top_idx < STACK_SIZE) 
     {
        stack[top_idx] = item;
        top_idx++;
     }
}

char
pop (char *stack)        /* input/output - pointer to top of stack */
{
    char item;       /* value popped off the stack */

    if (top_idx >= 0) 
    {
        top_idx--;
        item = stack[top_idx];        
    } 
    else 
    {
         item = STACK_EMPTY;
    }

    return (item);
}

int
main (void)
{
   char s [STACK_SIZE];

   push(s,'a');
   push(s,'b');

   printf("Pop = %c \n",pop(s));
   printf("Pop = %c \n",pop(s));
   
   

   return 0;
}

The error regarding " stack assumed to have one element" is because you put no number between the square brackets char stack[];关于“假定有一个元素的stack ”的错误是因为您在方括号char stack[]; . . I suspect you meant我怀疑你的意思

char stack[STACK_SIZE];

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当我尝试在Linux而不是Mac上进行编译时,为什么会出现“错误:函数&#39;fileno&#39;的隐式声明” - Why am I getting “error: implicit declaration of function ‘fileno’” when I try to compile on Linux but not on a Mac 尝试运行测试时,为什么会出现“ Segmentation Fault”错误? - Why am I getting a “Segmentation Fault” error when I try to run the tests? 当我尝试释放分配的指针时,为什么会出现无效指针错误? - Why am I getting an invalid pointer error when I try to free malloced pointers? 当我尝试使用 fopen 时,我从 valgrind 获得了大小为 1 的无效读取错误 - I am getting an invalid read of size one error from valgrind when I try to use fopen 为什么在函数中使用结构和枚举时会出错? - Why am I getting error when using Structs and Enums in Function? 当我从 function 返回时尝试打印新数组时,我得到了垃圾 - I am getting junk when I try to print the new array when i return it from function 将数字传递给期望枚举的函数时,为什么没有得到错误(或至少警告)? - Why am I not getting an error (or at least warning) when I pass a number to a function that is expecting an enum? 为什么我在使用 Strcmp 时没有收到 output? - Why am I not getting the output when I use Strcmp? 为什么我收到“错误:使用未声明的标识符”错误? - Why am I getting “error: use of undeclared identifier” error? 错误:没有以前的功能原型。 为什么我收到此错误? - Error: No previous prototype for function. Why am I getting this error?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM