Label related compiler error

Been looking around, and I didn't find it... Is there a way to prototype a label in C/C++ for Arduino? For some reason Arduino will only "goto" to a label that is formerly declared (higher in the code). It throws errors when you try to goto a later place.

This works:

LABEL:  
goto LABEL;

This does not work:

goto LABEL;
LABEL:

Program:

void setup(){

}

void loop(){
  goto LABEL;
  LABEL:
}

Compiler errors:

Program.ino: In function 'void loop()':
Program:9: error: expected primary-expression before '}' token
Program:9: error: expected `;' before '}' token

And please do not talk about why labels are bad, or why I should not be using them.

And please do not talk about why labels are bad, or why I should not be using them.

Why not? I've been programming in C and C++ for 30 years, with 100s of 1000s of lines of code, and not a single goto in any of that. The number of legitimate uses of goto in the whole lot of C code can probably be counted on one hand. Maybe two. I seriously doubt that anything you are doing on the Arduino NEEDS goto.

Because I don't want this thread to be about the pros and cons of labels and gotos. I want to be able to use them, and I'd like to know why it doesn't work.

The error message was pretty explicit:

Program:9: error: expected `;' before '}' token

Try inserting a ';' before the '}'

Also, try to not use goto. (Couldn't resist, sorry).

PaulS: The number of legitimate uses of goto in the whole lot of C code can probably be counted on one hand.

One finger, I suggest.

Thanks, that does compile, but why would I need to do that (put a semi-colon after the colon)?

goto LABEL; LABEL:;

Other than the file name, function name, and line number, the Arduino error messages are almost always useless, so I tend to disregard them.

Thanks, that does compile, but why would I need to do that (put a semi-colon after the colon)?

Because a label introduces a block of code. You've got to have a block of code after the label. The block can be one line, which can have nothing but a semicolon on it.

Now, why, given that goto is not necessary, do you think you need to use it?

Okay, I guess that makes sense.

I don't think I need to use it. I was simply playing around, and ran into the error.

mattallen37: And please do not talk about why labels are bad, or why I should not be using them.

They are bad, and you should not be using them.

As an aside, labels have to be associated with a statement and cannot be used in isolation; in your failing examples, there's no statement after the label.

I know that they aren't necessary, but sometimes it's fun to play around with them.

:roll_eyes: this is turning into what I explicitly stated I didn't want it to...

PaulS:

Thanks, that does compile, but why would I need to do that (put a semi-colon after the colon)?

Because a label introduces a block of code. You've got to have a block of code after the label. The block can be one line, which can have nothing but a semicolon on it.

Picking nits, a labeled statement is a statement preceded by a label name and colon (or the 'case' keyword followed by an integer constant and then a colon or the 'default' keyword followed by a colon). Since a labeled statement is one of the statement types, you can have multiple labels that ultimately precede a normal statement. If the statement isn't a compound statement or loop, you need a ';' at the end. A ';' by itself is an empty statement. In the ISO C 99 standard this is in section 6.8.1 of the standard. I don't have an online copy of the C++ standard, so I don't know the chapter and verse in C++, but C++ follows similar rules.

mattallen37: this is turning into what I explicitly stated I didn't want it to...

I know, but sometimes people ask how to do things, like "how to I turn interrupts off because of blah" and it turns out that the "real" solution is to dissuade them from the thing they were attempting in the first place.

It would be like going to a car manufacturer and asking how to disable the airbags.

"But that's not a good idea," they would say.

"But I want to," you reply.

"But why?" they ask.

"I just want to experiment," you reply. "It will be fun to drive around with them disabled."

It's not too hard to actually read the error message.

[quote author=Nick Gammon link=topic=146205.msg1098454#msg1098454 date=1359674271]

mattallen37:
this is turning into what I explicitly stated I didn’t want it to…

I know, but sometimes people ask how to do things, like “how to I turn interrupts off because of blah” and it turns out that the “real” solution is to dissuade them from the thing they were attempting in the first place.

It would be like going to a car manufacturer and asking how to disable the airbags.

“But that’s not a good idea,” they would say.

“But I want to,” you reply.

“But why?” they ask.

“I just want to experiment,” you reply. “It will be fun to drive around with them disabled.”

[/quote]Then I’d say “Fine, I’ll just pull the fuse” << XD … or more likely go online and ask someone who will (hopefully) cut to the chase and actually tell me the answer to what I am asking, instead of telling me all the reasons it’s so bad to do it.

OK. Let's try this. Go ahead. Load your code up with impossible to follow goto statements and labels. Then, when you have a problem with it not performing as expected:

goto someplaceElseForHelp:
1 Like

ask someone who will (hopefully) cut to the chase and actually tell me the answer to what I am asking, instead of telling me all the reasons it's so bad to do it.

There's an expectation when posting answers here that they're not just for the benefit of the OP. Other folks will take note of what's written, so you'll always get the reasons why goto is bad recited if you ask about using it, just as most any question about dealing with mains voltage garners a warning about the possible dangers.

This. It's like asking how to clean a loaded gun. If the answer didn't include a lot of warnings, the implication would be that the experts think that is a good thing to do.

PaulS: OK. Let's try this. Go ahead. Load your code up with impossible to follow goto statements and labels. Then, when you have a problem with it not performing as expected:

goto someplaceElseForHelp:

Plus what PaulS said.

mattallen37: actually tell me the answer to what I am asking, instead of telling me all the reasons it's so bad to do it.

It's "as well as", rather than "instead of".

You asked why it didn't work; you've been told. You've also been told that what you were trying to do is not recommended - which is true, however much you may dislike it.

I thought Nick was joking about asking how to disable the airbags, so I just continued on it.

In the first post I requested an answer to a compiler error, and requested that nobody talk about the downsides of labels and gotos. Well over half the posts have been about something OT from my question, or irrelevant to me finding the answer. I implied right off the bat that labels and gotos are not recommended, which other people reading this thread in the future should have picked up on.

IMO these forums are great for sharing information, and helping people out, but it sure takes from the experience to often have people (seemingly) go out of their way to annoy me, or just troll/spam the thread. I'm not sure how many threads I have started, only to have a majority of the replies be OT from my question(s), or otherwise mostly useless.

I've been within the top three posters on about 4 tech-related forums (each for over a year), since I was about 14 YO, and I can honestly say that the Arduino forums are not a pleasure to use. I go out of my way to avoid these forums, and only post here when I can't find the answer anywhere else, because I know how welcome you make me feel. I really really wish that was not the case =(

Hopefully next time I post a question here, things will go smoother.

A lot of people say how helpful these forums are, I’m sorry your experience hasn’t been as positive.

Look on the bright side, we are trying to help you, not annoy you.

The thing is, and we see this quite often, and today there is another thread like this, the more people ask a really technical question, without in any way relating it to a real-world problem, the more likely we are to find, three pages on, that they have gone about the actual problem (so far a secret) in a way that is … how shall I put this? … not optimal.

I remember now, there is another thread about "which is faster: ‘while’ or ‘for’ "?

On the face of it, it is a reasonable question, but there is a lot unsaid there. Does it matter? What is being attempted? Maybe the underlying hardware makes the question irrelevant.

Part of the service is not just answering the literal question, but trying to see past it to actually helping you solve the underlying problem.