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

标签 c gcc 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,您可以使用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/

相关文章:

c - 我的递归质数函数代码有一些错误

c++ - 具有分层访问控制的存储库的 SVN 或 Git

c - 如何在我的 write() 超时时破坏性地关闭套接字?

c - 我如何在汇编中编写所有这些

c - c程序中的段错误

带有goto命令的windows批处理文件不起作用

c - 在 C 中查找 n x n 数组的第一行零

c - 对 char 数组中存储的字节使用 strtol

c - 用于计算整数中 1 位的递归程序

linux - 了解事件是否发生(运行 gtk_dialog_run() 时)