使用 C 跳转到代码中的特定行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7375446/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 16:51:00  来源:igfitidea点击:

Jumping to a specific line in code using C

c++c

提问by adammenges

Sorry if this question is silly, but I can't seem to find the answer on Google. I haven't done C programing in a while and for the life of me remember how, in C or C++, to jump back (or forward) to a specific line in code. If I remember right, there was a way to do this.

对不起,如果这个问题很愚蠢,但我似乎无法在谷歌上找到答案。我已经有一段时间没有进行 C 编程了,我一生都记得如何在 C 或 C++ 中跳回(或前进)到代码中的特定行。如果我没记错的话,有一种方法可以做到这一点。

Thanks for the help!

谢谢您的帮助!

Cheers!

干杯!

回答by K-ballo

The infamous goto, in conjuntion with labels.

臭名昭著的 goto,与标签相结合。

label_name:
goto label_name;

Before using it, search for 'goto considered harmful'.

在使用它之前,搜索“goto 被认为有害”。

回答by bdonlan

C and C++ do not have a concept of 'lines' after the preprocessing stage. As such, you cannot 'jump' to a line of code.

C 和 C++ 在预处理阶段之后没有“行”的概念。因此,您不能“跳转”到一行代码。

If you want to jump to a line of code in your editor, this depends on what editor you are using. If you want to jump to a specific statement(not line) at runtime, you could use goto, but this should be avoided for most circumstances, as it leads to difficult to understand code, and other control-flow structures are more appropriate in most cases.

如果您想跳转到编辑器中的一行代码,这取决于您使用的编辑器。如果你想在运行时跳转到一个特定的语句(不是line),你可以使用,但在大多数情况下应该避免这样做,因为它会导致难以理解代码,而其他控制流结构在大多数情况下更合适.goto

回答by Tony The Lion

If you want the nasty and easy solution: gotoand then a label which indicates the line of code you want to go to.

如果您想要讨厌且简单的解决方案:goto然后是一个标签,指示您要转到的代码行。

Normally though, you'd have a function with the specific functionality you want to invoke, and call that function.

但是,通常情况下,您会拥有一个具有要调用的特定功能的函数,然后调用该函数。

回答by Maxpm

Use a gotostatement, like this:

使用goto语句,如下所示:

goto SomeLine;

// Code code code...

SomeLine:

Note that this is considered extremely bad practice. Chances are, there's a better way to organize your code that avoids the need altogether.

请注意,这被认为是非常糟糕的做法。很有可能,有一种更好的方法来组织您的代码,从而完全避免这种需要。

回答by Alexis Wilke

I do also think that the goto instruction should not be used when you can help it.

我也认为 goto 指令在您可以提供帮助的情况下不应该使用。

However, contrary to many other languages, there isn't a neat way to exit from multi-layered if() blocks and the easiest is to the goto in this case.

然而,与许多其他语言相反,没有一种巧妙的方法可以退出多层 if() 块,在这种情况下最简单的方法是转到 goto。

What I suggest is that you add comments and use wisely named labels (l1: ... goto l1; sucks.)

我的建议是您添加评论并使用明智命名的标签(l1:...转到l1;糟透了。)

Note that people will tell you that the goto is bad, and use the break and continue statements in loops like crazy. They have the exact same sideeffect as the goto instruction and could be considered as bad (but aren't).

请注意,人们会告诉您 goto 不好,并且会疯狂地在循环中使用 break 和 continue 语句。它们具有与goto 指令完全相同的副作用,可以被认为是坏的(但不是)。

回答by George Gaál

There are some options to do it: 'goto' instruction, setjmp/longjmp functions. Also you can use in c++ SEH (exception propagation and handling), but it isn't simple. And if you really nead such thing as jumping to particular line of code, I will recommend you to rewrite your code using loops and conditionals with additional state variables. Because you have serious problem with structure or design of your code. It may look ugly but it is more safer than using goto and other "black magic"

有一些选项可以做到:“goto”指令、setjmp/longjmp 函数。您也可以在 c++ SEH(异常传播和处理)中使用,但这并不简单。如果您真的需要跳转到特定代码行这样的事情,我会建议您使用带有附加状态变量的循环和条件来重写代码。因为您的代码结构或设计存在严重问题。它可能看起来很丑,但它比使用 goto 和其他“黑魔法”更安全