gpt4 book ai didi

c - "expected expression"goto 标签错误(非本地 goto)

转载 作者:行者123 更新时间:2023-12-04 10:46:38 30 4
gpt4 key购买 nike

是的,我知道 Esdger Dijkstra 说你不应该使用 goto ,但他不是上帝。我认为只要不过分使用无条件分支就可以了。过度使用继承和过度使用分支语句一样有可能创建不可读的代码。

无论如何,现在我已经完成了先发制人的反驳,这是我正在编写的程序。它应该从文件中读取代码(最终我希望它读取 HTML 代码,但我现在使用我自己的简化标记语言因为它更容易)并将其转换为可用作 enscript 程序输入的格式.

#include <stdio.h>
#include <stdlib.h>

#define INPUT_ERROR 1

void writecolor( FILE *, float, float, float );
unsigned short hextonum( char );

char escape = '\0'; // Default for enscript

int main( int argc, char **argv ){
FILE *in;
FILE *out;
if( argv[1] ){
in = fopen( argv[1], "r" );
}
else{
in = stdin;
}
if( argv[2] ){
out = fopen( argv[2], "w" );
}
else{
out = stdout;
}
char c; // Input character
float red; // Red value from hex code
float green; // Green value from hex code
float blue; // Blue value from hex code
int line = 1; // Current line number, used for error reporting
while( (c = fgetc( in )) != EOF ){
if( c == '\\' ){
if( fgetc( in ) == '#' ){
red = (float) hextonum( fgetc( in ) ) * 10 / 16 * 0.1 + (float) hextonum( fgetc( in ) ) * 10 / 16 * 0.01;
green = (float) hextonum( fgetc( in ) ) * 10 / 16 * 0.1 + (float) hextonum( fgetc( in ) ) * 10 / 16 * 0.01;
blue = (float) hextonum( fgetc( in ) ) * 10 / 16 * 0.1 + (float) hextonum( fgetc( in ) ) * 10 / 16 * 0.01;
writecolor( out, red, green, blue );
}
}
else{
fputc( c, out );
}
if( c == '\n' ){
line++;
}
}
fclose( in );
fclose( out );
return 0;
:infile_error // XXX goto in hextonum branches here
fprintf( stderr, "%s: Error on line %d of input file.\n", argv[0], line );
return INPUT_ERROR;
}

// Converts a color code in the markup to a color code
// recognized by enscript
void writecolor( FILE *fp, float red, float green, float blue ){
fwrite( &escape, 1, 1, fp );
fprintf( fp, "color{%f %f %f}", red, green, blue );
}

// Converts a hex digit to its corresponding value
unsigned short hextonum( char hex ){
if( hex >= '0' && hex <= '9' ){
return atoi( hex );
}
switch( hex ){
case( 'a' ) : case( 'A' ) : return 0xa;
case( 'b' ) : case( 'B' ) : return 0xb;
case( 'c' ) : case( 'C' ) : return 0xc;
case( 'd' ) : case( 'D' ) : return 0xd;
case( 'e' ) : case( 'E' ) : return 0xe;
case( 'f' ) : case( 'F' ) : return 0xf;
}
// Okay, I think rather than putting an if statement
// around every piece of code that uses this function,
// I'm just going to jump to an error code in the
// main function.
goto infile_error;
}

这是一项正在进行中的工作,目前还远未达到完美甚至功能。我只是想知道为什么我不断收到以下错误:

html2enscript.c:50:2: error: expected expression
:infile_error // XXX goto in hextonum branches here
^

这是编译器强制执行的某种良好实践规则,还是我确实做错了什么(我说的错误是指语法错误,而不仅仅是糟糕的编程风格)?

附加信息:

这是我的 gcc 版本信息:

Configured with: --prefix=/Library/Developer/CommandLineTools/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
Apple LLVM version 7.3.0 (clang-703.0.31)
Target: x86_64-apple-darwin15.0.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin

最佳答案

这里有两个问题。第一个是您的 goto 标签指定不正确。

你有这个:

:infile_error

应该是这样的:

infile_error:

第二个问题较大,是您试图使用 goto 跳转到另一个函数。这是不允许的。

看来您正在尝试实现某种异常机制。然而,这在 C 中不受支持。

执行此操作的正确方法是让函数返回一些指定错误的值,然后在函数退出时检查该值。

如果您真的想要执行非本地goto,您可以使用setjmplongjmp >。这与 C 中的异常处理非常接近。

jmp_buf hextonum_err;

int main( int argc, char **argv ){
...
if (setjmp(hextonum_err) != 0) {
goto infile_error;
}
while( (c = fgetc( in )) != EOF ){
...
}

unsigned short hextonum( char hex ){
if( hex >= '0' && hex <= '9' ){
// don't use atoi here as that expects a string
return hex - '0';
}
switch( hex ){
case( 'a' ) : case( 'A' ) : return 0xa;
case( 'b' ) : case( 'B' ) : return 0xb;
case( 'c' ) : case( 'C' ) : return 0xc;
case( 'd' ) : case( 'D' ) : return 0xd;
case( 'e' ) : case( 'E' ) : return 0xe;
case( 'f' ) : case( 'F' ) : return 0xf;
}
longjmp(hextonum_err, 1);
// never reached, but put here because compiler will warn on non returning a value
return 0;
}

一般来说,我不推荐这样写代码。

关于c - "expected expression"goto 标签错误(非本地 goto),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40247209/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com