Ç - 无法堆放在我的计划启动

问题描述 投票:1回答:1

我想创建一个堆栈,但我有启动它的一个问题。我有代码是:

#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依然。我真的不明白。难道是因为指针?

c arrays struct stack typedef
1个回答
3
投票

声明:

void initstk(stack *s1);
/*...*/
int main() {
stack s1;

但你作为调用:

initstk(s1);

由于initstk需要一个指针参数,你应该通过S1的地址:

initstk(&s1);

我很惊讶你的编译器没提醒你关于不匹配。

© www.soinside.com 2019 - 2024. All rights reserved.