7_13发现了一个小问题为啥在do_line函数里赋值语句在变量定义语句之前就会报错?

1.在变量定义之前出错:

<bldc:/home/tingbinz/apue.3e/SBSCODE/7>R*_*G:vim 7_13.c
  1 #include "apue.h"
  2 #include <setjmp.h>
  3 #define TOKEN 5
  4 void do_line (char *ptr);
  5 void cmd_add (void);
  6 int get_token(void);
  7 #define MAXLINE 1024
  8
  9 jmp_buf jmpbuffer;
 10
 11 int main()
 12 {
 13         char line[MAXLINE];
 14         if (setjmp(jmpbuffer) != 0)
 15                 printf("error");
 16         while (fgets(line,MAXLINE,stdin) != NULL){
 17                 do_line(line);
 18         }
 19         exit(0);
 20 }
 21
 22 char *tok_ptr;
 23
 24 void do_line(char *ptr)
 25 {
 26         //tok_ptr = ptr;
 27         tok_ptr = ptr;
 28         int cmd;
 29
 30         //tok_ptr = ptr;
 31         while ( (cmd = get_token()) > 0){
 32                 switch (cmd){
 33                         case TOKEN:
 34                                 cmd_add();
 35                                 break;
 36                 }
 37         }
 38
 39 }
 40
 41 int get_token(void)
 42 {
 43         /* fetch the next token from the line pointed to by tok_ptr;*/
 44 }
 45
 46 void cmd_add(void)
 47 {
"7_13.c" 53L, 761C written
<bldc:/home/tingbinz/apue.3e/SBSCODE/7>R*_*G:gcc -Wall -ggdb3 setlongjmp 7_13.c
gcc: setlongjmp: No such file or directory
7_13.c:7: warning: `MAXLINE' redefined
apue.h:29: warning: this is the location of the previous definition
In file included from apue.h:132,
                 from 7_13.c:1:
error.c: In function `err_doit':
error.c:121: warning: implicit declaration of function `vsnprintf'
error.c:123: warning: implicit declaration of function `snprintf'
7_13.c: In function `do_line':
7_13.c:28: parse error before `int'
7_13.c:31: `cmd' undeclared (first use in this function)
7_13.c:31: (Each undeclared identifier is reported only once
7_13.c:31: for each function it appears in.)
7_13.c:34: warning: unreachable code at beginning of switch statement
7_13.c: In function `get_token':
7_13.c:44: warning: control reaches end of non-void function


挪到变量定义之后 不再出错。
<bldc:/home/tingbinz/apue.3e/SBSCODE/7>R*_*G:vim 7_13.c
  1 #include "apue.h"
  2 #include <setjmp.h>
  3 #define TOKEN 5
  4 void do_line (char *ptr);
  5 void cmd_add (void);
  6 int get_token(void);
  7 #define MAXLINE 1024
  8
  9 jmp_buf jmpbuffer;
 10
 11 int main()
 12 {
 13         char line[MAXLINE];
 14         if (setjmp(jmpbuffer) != 0)
 15                 printf("error");
 16         while (fgets(line,MAXLINE,stdin) != NULL){
 17                 do_line(line);
 18         }
 19         exit(0);
 20 }
 21
 22 char *tok_ptr;
 23
 24 void do_line(char *ptr)
 25 {
 26         //tok_ptr = ptr;
 27         int cmd;
 28
 29         tok_ptr = ptr;
 30         while ( (cmd = get_token()) > 0){
 31                 switch (cmd){
 32                         case TOKEN:
 33                                 cmd_add();
 34                                 break;
 35                 }
 36         }
 37
 38 }
 39
 40 int get_token(void)
 41 {
 42         /* fetch the next token from the line pointed to by tok_ptr;*/
 43 }
 44
 45 void cmd_add(void)
 46 {
 47         int token;
"7_13.c" 52L, 743C written
<bldc:/home/tingbinz/apue.3e/SBSCODE/7>R*_*G:gcc -Wall -ggdb3 setlongjmp 7_13.c
gcc: setlongjmp: No such file or directory
7_13.c:7: warning: `MAXLINE' redefined
apue.h:29: warning: this is the location of the previous definition
In file included from apue.h:132,
                 from 7_13.c:1:
error.c: In function `err_doit':
error.c:121: warning: implicit declaration of function `vsnprintf'
error.c:123: warning: implicit declaration of function `snprintf'
7_13.c: In function `get_token':
7_13.c:43: warning: control reaches end of non-void function




求解释。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值