为什么我的堆栈数据结构出现错误?

Why am I getting errors in my stack data structure?

我决定用 C 做一个堆栈。我刚写了一个 push 函数的代码,现在我收到错误(它说我的 maxsize).

如果你问我一切似乎都刚刚好,我不知道会出现什么错误。

#include <stdio.h>
#include <stdlib.h>

#define maxsize 100;

int top=-1, item;
int s[maxsize];

void push()
{
    if (top == maxsize-1)
    {
        printf("Stack overflow\n");
        return;
    }

    top = top + 1;
    s[top] = item;
}

我得到的错误是:-

stack.c:3:20: error: expected ']' before ';' token
#define maxsize 100;
               ^
stack.c:5:7: note: in expansion of macro 'maxsize'
int s[maxsize];
  ^~~~~~~
stack.c: In function 'push':
stack.c:3:20: error: expected ')' before ';' token
#define maxsize 100;
               ^
stack.c:9:16: note: in expansion of macro 'maxsize'
if (top == maxsize-1)
           ^~~~~~~
stack.c:9:8: note: to match this '('
if (top == maxsize-1)
   ^
stack.c:16:5: error: 's' undeclared (first use in this function)
s[top]=item;
^
stack.c:16:5: note: each undeclared identifier is reported only once
for each function it appears in

#define 指令不以 ; 结尾。更改为 #define maxsize 100

使用此代码:

#include <stdio.h>
#include <stdlib.h>

#define maxsize 100

int top=-1,item;
int s[maxsize];

void push()
{
    if (top == maxsize-1)
    {
        printf("Stack overflow\n");
        return;
    }

    top = top + 1;
    s[top] = item;
}

在这里,您在导致问题的 #define 语句之后使用了 ;