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

来源:互联网 发布:三菱fx2n pid编程手册 编辑:程序博客网 时间:2024/05/29 17:06

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




求解释。

0 0
原创粉丝点击