在C中为什么你需要一个goto标签后的声明?

Ant*_*ado 30 c gcc

我正在编写一些C代码,在我的代码中我有两个嵌套循环.在特定条件下,我想要break从内循环和continue外循环.我尝试使用外部循环代码末尾的标签来实现这一点,并且条件是goto该标签.但是gcc给出了一个错误,我在复合语句的末尾没有标签.为什么不?

注1:这不是switch声明,问题已在其他地方得到解答.

注2:这不是关于样式的问题,而是我应该或不应该使用goto语句或条件变量.

编辑:人们已经要求一个例子,我可以给出一个稍微容易的例子,检查数组是否是另一个数组的子数组

    int superArray[SUPER_SIZE] = {...}, subArray[SUB_SIZE] = {...};
    int superIndex, subIndex;

    for (superIndex=0; superIndex<SUPER_SIZE-SUB_SIZE; superIndex+=1)
    {
      for (subIndex=0; subIndex<SUB_SIZE; subIndex+=1)
        if (superArray[superIndex+subIndex] != subArray[subIndex])
          goto break_then_continue;

      // code that executes if subArray is a sub array

      break_then_continue:
    }
Run Code Online (Sandbox Code Playgroud)

Fil*_*efp 38

在标准中,它明确表示标签属于一个语句,因此;标签之后的一个简单的分号()可以绕过你运行的问题,因为它算作一个语句.

6.8.3/6中甚至有一个使用" " 1语句的例子.

示例3 null语句也可用于在复合语句的结束之前携带标签

while (loop1) {
  /* ... */

  while (loop2) {
    /* ... */

    if (want_out)
      goto end_loop1;

    /* ... */
  }

  /* ... */

  end_loop1: ;
}
Run Code Online (Sandbox Code Playgroud)

1 在标准中,这被称为a null statement.


6.8.1标记语句

Syntax
  1 labeled-statement:
      identifier : statement
      case constant-expression : statement
      default : statement
Run Code Online (Sandbox Code Playgroud)

请注意,statement在上述报价中不是可选的.



Jon*_*ler 5

您只需要编写:

label: ;
Run Code Online (Sandbox Code Playgroud)

分号是一个空语句。您需要它,因为语言是这样定义的;即使是空的,您也需要查看一条语句。

    for (int i = 0; i < N; i++)
    {
        for (int j = 0; i < M; j++)
        {
            ...
            if (some_condition)
                goto continue_loop1;
            ...
        }
continue_loop1: ;
    }
Run Code Online (Sandbox Code Playgroud)

您可以争论标签上的缩进。


oua*_*uah 5

标签应该指向一个语句。

C 规定:

(C99, 6.8.1 Labeled statements p4)“任何语句前面都可以有一个前缀,将标识符声明为标签名称。”

在您的情况下,您可以使用 null 语句:

void foo(void)
{
    goto bla;

    bla:
    ;
 }
Run Code Online (Sandbox Code Playgroud)

空语句不执行任何操作。

或者,如果您有声明,也可以使用复合语句(块):

void foo(void)
{
    goto bla;

    bla:
    {
        int x = 42;
        printf("%d\n", x);
    }
 }
Run Code Online (Sandbox Code Playgroud)