【发布时间】:2019-02-05 02:10:55
【问题描述】:
我正在尝试创建一个堆栈,但我在启动它时遇到了问题。我的代码是:
#define LINELN 72
#define STACKSZ 25
#define NEWLN '\n'
#include <stdlib.h>
#include <stdio.h>
// interface struct for stack
typedef struct stack {
char data[STACKSZ];
int top;
} stack;
void initstk(stack *s1);
int emptystk(stack s);
int main() {
stack s1;
initstk(s1);
printf("%d",emptystk(s1));
exit(0);
}
void initstk(stack *s1) {
s1->top=-1;
}
int emptystk(stack s) {
if(s.top == -1){
return 1;
}
else{
return 0;
}
}
我希望它打印出 1,因为堆栈是空的,但它仍然打印出 0。我真的不明白。会不会是指针的原因?
【问题讨论】:
-
我赞成你的问题,因为你发布了一个很好的Minimal, Complete, and Verifiable example 谢谢!!!
标签: c arrays struct stack typedef