是否有可能以一个标号的地址存储在变量和使用goto语句跳转到了吗? [英] Is it possible to store the address of a label in a variable and use goto to jump to it?

查看:160
本文介绍了是否有可能以一个标号的地址存储在变量和使用goto语句跳转到了吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道每个人都讨厌的goto。在我的code,原因我已经考虑并很舒服,他们提供了一个有效的解决方案(即我不是在寻找不这样做作为一个答案,我理解你的预订,并理解我为什么我使用他们无论如何)。

到目前为止,他们已经很棒,但我想在需要我基本上能够指针存储到标签,然后去找他们,以后这样的方式来扩充功能。

如果这个code的工作,将重新present的功能,我需要的类型。但它不工作,和谷歌上搜索30分钟没有透露什么。没有人有任何想法?

  INT主要(无效)
{
  INT I = 1;
  无效* the_label_pointer;  标签:  the_label_pointer =安培; the_label;  如果我 - )
    转到* the_label_pointer;  返回0;
}


解决方案

C和C ++标准不支持此功能。然而,GNU编译器集合(GCC)包括非标准扩展这样做的如这文章。从本质上讲,他们增加了一个特殊的运算符&放大器;&安培;一个报告的标签类型无效*的地址。看到这篇文章的详细信息。

P.S。换句话说,只要使用&放大器;&安培;代替与&在你的榜样,它会在GCC工作。

P.P.S.我知道你不希望我这么说,但我会说也无妨,......不这样做!

I know everyone hates gotos. In my code, for reasons I have considered and am comfortable with, they provide an effective solution (ie I'm not looking for "don't do that" as an answer, I understand your reservations, and understand why I am using them anyway).

So far they have been fantastic, but I want to expand the functionality in such a way that requires me to essentially be able to store pointers to the labels, then go to them later.

If this code worked, it would represent the type of functionality that I need. But it doesn't work, and 30 min of googling hasn't revealed anything. Does anyone have any ideas?

int main (void)
{
  int i=1;
  void* the_label_pointer;

  the_label:

  the_label_pointer = &the_label;

  if( i-- )
    goto *the_label_pointer;

  return 0;
}

解决方案

The C and C++ standards do not support this feature. However, the GNU Compiler Collection (GCC) includes a non-standard extension for doing this as described in this article. Essentially, they have added a special operator "&&" that reports the address of the label as type "void*". See the article for details.

P.S. In other words, just use "&&" instead of "&" in your example, and it will work on GCC.
P.P.S. I know you don't want me to say it, but I'll say it anyway,... DON'T DO THAT!!!

这篇关于是否有可能以一个标号的地址存储在变量和使用goto语句跳转到了吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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