I use gcc version 4.3.2 (Debian 4.3.2-1.1). I wrote a simple program in C to implement and test a integer stack. Stack is implemented by the STACK structure. I used a constant named STACKSIZE to define the STACK's size. My program code looks like this:
#include<stdio.h>
#include<stdlib.h>
#define STACKSIZE 10;
typedef struct {
int size;
int items[STACKSIZE];
} STACK;
void push(STACK *ps, int x)
{
if (ps->size == STACKSIZE) {
fputs("Error: stack overflow\n", stderr);
abort();
} else
ps->items[ps->size++] = x;
}
int pop(STACK *ps)
{
if (ps->size == 0){
fputs("Error: stack underflow\n", stderr);
abort();
} else
return ps->items[--ps->size];
}
int main() {
STACK st;
st.size = 0;
int i;
for(i=0; i < STACKSIZE + 1; i++) {
push(&st, i);
}
while(st.size != 0)
printf("%d\n", pop(&st));
printf("%d\n", pop(&st));
return 0;
}
when i used
#define STACKSIZE 10;
gcc would return following errors:
ex_stack1.c:8: error: expected ‘]’ before ‘;’ token
ex_stack1.c:9: warning: no semicolon at end of struct or union
ex_stack1.c: In function ‘push’:
ex_stack1.c:13: error: expected ‘)’ before ‘;’ token
ex_stack1.c:17: error: ‘STACK’ has no member named ‘items’
ex_stack1.c: In function ‘pop’:
ex_stack1.c:26: error: ‘STACK’ has no member named ‘items’
ex_stack1.c: In function ‘main’:
ex_stack1.c:33: error: expected ‘)’ before ‘;’ token
when i used
const int STACKSIZE=10;
gcc would return following error:
ex_stack1.c:8: error: variably modified ‘items’ at file scope
when i used
enum {STACKSIZE=10};
gcc would compile my program successfully.
What happenned? How should i modify my program to use
#define STACKSIZE 10;
or
const int STACKSIZE=10;