奇怪了,C语言函数声明的问题。

来源:互联网  时间:2016/8/21 16:36:47

关于网友提出的“ 奇怪了,C语言函数声明的问题。”问题疑问,本网通过在网上对“ 奇怪了,C语言函数声明的问题。”有关的相关答案进行了整理,供用户进行参考,详细问题解答如下:

问题: 奇怪了,C语言函数声明的问题。
描述:

源程序(模拟堆栈)如下:
象现在这样,在TURBOC2下编译就通不过,而我把声明部分提到main函数前就可以了,请问是何缘故?????
#define M 5
#include 
struct stack
{ int s[M];
  int top;
  };
/*push(struct stack *st_push,int x_push);
pop(struct stack *st_pop);*/
main()
{
  struct stack y;
  int i,z=0;
  y.top=0;
  push(struct stack *st_push,int x_push);
  pop(struct stack *st_pop);
  for(i=0;i<>
      { scanf("%d",&z);
push(&y,z);
      }
  for(i=0;i<>
      { z=pop(&y);
printf("%d ",z);
      }
}
push(struct stack *st_push,int x_push)
{ if(st_push->top==M)
    { printf("\noverflow\n");
      return(0);
    }
  st_push->s[st_push->top]=x_push;
  st_push->top++;
  return(1);
}
pop(struct stack *st_pop)
   { if(st_pop->top==0)
       { printf("stack empty");
 return(0);
       }
     st_pop->top--;
     return(st_pop->s[st_pop->top]);
   } 


解决方案1:

这个前面jy13已经说过了吧?
C语言要求声明要在正式的执行语句之前,
main()
{
。。。
  y.top=0;   你加这一句当然不行啦
  push(struct stack *st_push,int x_push);

解决方案2:

C/C++不同于Pascal语言,C/C++不属性块类语言,因此它不能够函数中套有函数,所以函数均为独立的,包括main函数也一样,且所有的函数使用前,必须先声明(main函数有点特殊)。

上一篇怎样在一个DLL中调用另一个DLL中的方法
下一篇一个看似十分简单的问题?
明星图片
相关文章
《 奇怪了,C语言函数声明的问题。》由码蚁之家搜集整理于网络,
联系邮箱:mxgf168#qq.com(#改为@)