[英]“expected expression” error on goto label (non-local goto)
是的,我知道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
,你可以用setjmp
和longjmp
來做。 這與你在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;
}
一般來說,我不建議像這樣編寫代碼。
正如我在評論中提到的,你不能使用goto
跨功能邊界分支,所以這不起作用。
就個人而言,我將紅色/綠色/藍色計算抽象為自己的函數並檢查結果:
int readcolor( FILE *stream, float *red, float *green, float *blue )
{
float *rgb[3];
rgb[0] = red;
rgb[1] = green;
rgb[2] = blue;
for ( size_t i = 0; i < 3; i++ )
{
int b0 = fgetc( stream );
int b1 = fgetc( stream );
if ( b0 == EOF || b1 == EOF )
return 0;
*rgb[i] = hextonum( b0 ) * 10.0f / 16.0f * 0.1f +
hextonum( b1 ) * 10.0f / 16.0f * 0.1f;
}
return 1;
}
然后在你的main
功能:
if( fgetc( in ) == '#' )
{
if ( readcolor( in, &red, &green, &blue ) )
writecolor( out, red, green, blue );
}
else
{
...
}
無論如何,使main
代碼掃描更好一些。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.