0
Offline
Full Member
Karma: 0
Posts: 127
Arduino rocks
|
 |
« on: January 31, 2013, 05:35:25 pm » |
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.
|
|
|
|
|
Logged
|
|
|
|
|
Seattle, WA USA
Online
Brattain Member
Karma: 334
Posts: 36442
Seattle, WA USA
|
 |
« Reply #1 on: January 31, 2013, 05:44:25 pm » |
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.
|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
Full Member
Karma: 0
Posts: 127
Arduino rocks
|
 |
« Reply #2 on: January 31, 2013, 05:52:40 pm » |
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.
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
Melbourne, Australia
Offline
Shannon Member
Karma: 226
Posts: 14101
Lua rocks!
|
 |
« Reply #3 on: January 31, 2013, 05:54:17 pm » |
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).
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
Melbourne, Australia
Offline
Shannon Member
Karma: 226
Posts: 14101
Lua rocks!
|
 |
« Reply #4 on: January 31, 2013, 05:59:29 pm » |
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.
|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
Full Member
Karma: 0
Posts: 127
Arduino rocks
|
 |
« Reply #5 on: January 31, 2013, 05:59:58 pm » |
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.
|
|
|
|
|
Logged
|
|
|
|
|
Seattle, WA USA
Online
Brattain Member
Karma: 334
Posts: 36442
Seattle, WA USA
|
 |
« Reply #6 on: January 31, 2013, 06:02:48 pm » |
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?
|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
Full Member
Karma: 0
Posts: 127
Arduino rocks
|
 |
« Reply #7 on: January 31, 2013, 06:06:39 pm » |
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.
|
|
|
|
|
Logged
|
|
|
|
|
UK
Offline
Tesla Member
Karma: 100
Posts: 6784
-
|
 |
« Reply #8 on: January 31, 2013, 06:06:50 pm » |
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.
|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
Full Member
Karma: 0
Posts: 127
Arduino rocks
|
 |
« Reply #9 on: January 31, 2013, 06:11:01 pm » |
I know that they aren't necessary, but sometimes it's fun to play around with them.  this is turning into what I explicitly stated I didn't want it to...
|
|
|
|
|
Logged
|
|
|
|
|
Ayer, Massachusetts, USA
Offline
Edison Member
Karma: 28
Posts: 1145
|
 |
« Reply #10 on: January 31, 2013, 06:14:58 pm » |
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.
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
Melbourne, Australia
Offline
Shannon Member
Karma: 226
Posts: 14101
Lua rocks!
|
 |
« Reply #11 on: January 31, 2013, 06:17:51 pm » |
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."
|
|
|
|
|
Logged
|
|
|
|
|
Offline
God Member
Karma: 9
Posts: 839
|
 |
« Reply #12 on: January 31, 2013, 06:43:12 pm » |
It's not too hard to actually read the error message.
|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
Full Member
Karma: 0
Posts: 127
Arduino rocks
|
 |
« Reply #13 on: January 31, 2013, 06:45:42 pm » |
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." Then I'd say "Fine, I'll just pull the fuse" <<  ... 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.
|
|
|
|
|
Logged
|
|
|
|
|
Seattle, WA USA
Online
Brattain Member
Karma: 334
Posts: 36442
Seattle, WA USA
|
 |
« Reply #14 on: January 31, 2013, 06:55:21 pm » |
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:
|
|
|
|
|
Logged
|
|
|
|
|
|