在C语言中为什么你需要一个goto标签之后的声明? [英] In C why do you need a statement after a goto label?

查看:1698
本文介绍了在C语言中为什么你需要一个goto标签之后的声明?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写一些C code和我的code,我有两个嵌套循环。在特定的条件下我想出内部循环和继续外循环。我试图做到这一点用在标签上外环的code结束,条件,转到的标签。然而 GCC 给我不能在复合语句的结尾有一个标签错误。为什么不呢?

注1:这不是开关语句和的问题已回答<一个href=\"http://stackoverflow.com/questions/3455208/why-do-you-need-to-put-break-after-the-last-label-of-a-switch-statement\">elsewhere.

注2:这不是一个关于风格,我是否应该或不应该使用转到陈述或条件变量而不是问题。

编辑:有人问了一个例子,我可以给检查的一个稍微浅显的例子,如果一个数组是另一个数组的一个子

  INT superArray [SUPER_SIZE] = {...},子阵[SUB_SIZE] = {...};
    INT superIndex,分类指数;    为(superIndex = 0; superIndex&下; SUPER_SIZE-SUB_SIZE; superIndex + = 1)
    {
      对于(分类指数= 0;&分类指数LT; SUB_SIZE;子索引+ = 1)
        如果(superArray [superIndex +子索引]!=子阵[子索引])
          转到break_then_continue;      // code,它执行,如果子阵是一个子阵列      break_then_continue:
    }


解决方案

在标准对其进行了明确表示,标签属于一个说法,因此一个简单的分号(; )后您的标签可以绕过你在运行的问题,因为这算作一个声明。

有甚至是使用的一个例子是 1 在声明中的 6.8.3 / 6


  

例3 :一种空语句还可以用于携带一个标签只是
  复合语句的结束}之前


 而(循环1){
  / * ... * /  而(循环2){
    / * ... * /    如果(want_out)
      转到end_loop1;    / * ... * /
  }  / * ... * /  end_loop1:;
}

1 在标准这被称为空语句



  

6.8.1标记语句

 语法
  1标记的语句:
      鉴定网络连接器:声明
      案例不断-EX pression:声明
      默认:声明


注意语句不在上述引文可选的。


I am writing some C code and in my code I have two nested loops. On a particular condition I want to break out of the inner loop and continue the outer loop. I tried to achieve this using a label at the end on the outer loop's code and, on the condition, goto that label. However gcc gives an error that I cannot have a label at the end of a compound statement. Why not?

Note 1: This is not a switch statement and that question has been answered elsewhere.

Note 2: This is not a question about style and whether I should or should not be using goto statements or conditional variables instead.

EDIT: People have asked for an example and I can give a slightly facile example of checking if an array is a subarray of another array

    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:
    }

解决方案

In the standard it's explicitly said that labels belong to a statement, therefore a simple semicolon (;) after your label can circumvent the problem you are running in to, since that counts as a statement.

There is even an example of the use of an "empty"1 statement in 6.8.3/6.

EXAMPLE 3 A null statement may also be used to carry a label just before the closing } of a compound statement

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

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

    if (want_out)
      goto end_loop1;

    /* ... */
  }

  /* ... */

  end_loop1: ;
}

1 In the standard this is referred to as a null statement.


6.8.1 Labeled statements

Syntax
  1 labeled-statement:
      identifier : statement
      case constant-expression : statement
      default : statement

Notice that statement isn't optional in the above quotation.


这篇关于在C语言中为什么你需要一个goto标签之后的声明?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆