By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,028 Members | 1,784 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,028 IT Pros & Developers. It's quick & easy.

variable after label

P: n/a
Hi
I am using gcc

void peter(){
int x;
LABEL1:
int y; <--------------- Error
}

Why ? and how to fix it?

thanks
from Peter (cm****@hotmail.com)

Mar 14 '07 #1
Share this Question
Share on Google+
11 Replies


P: n/a
Hi
I am using gcc

void peter(){
int x;
LABEL1:
int y; <--------------- Error

}

But this is ok:
void peter(){
int x;
LABEL1:
; <---- Add an empty line here
int y;

}

Why ? and how to fix it?

thanks
from Peter (cm****@hotmail.com)

Mar 14 '07 #2

P: n/a
cm****@hotmail.com wrote:
Hi
I am using gcc

void peter(){
int x;
LABEL1:
int y; <--------------- Error

}

But this is ok:
void peter(){
int x;
LABEL1:
; <---- Add an empty line here
int y;

}
Why ?
You can only label statements. `int y;` isn't a statement.
and how to fix it?
Rip out the LABEL1:. The odds are /terribly/ good that you
don't need a label, even outside your example. Certainly
there are times where they're useful, just as there are
times when you need a rib-spreader or a bomb-disposal squad,
but that doesn't mean you have to spray them round your code
like baby-food.

--
Chris "electric hedgehog" Dollin
"A facility for quotation covers the absence of original thought." /Gaudy Night/

Mar 14 '07 #3

P: n/a
cmk...@hotmail.com wrote:
Hi
I am using gcc

void peter(){
int x;
LABEL1:
int y; <--------------- Error
I think a label must precede on or more statements. int y; isn't one.
>
}

But this is ok:
void peter(){
int x;
LABEL1:
; <---- Add an empty line here
int y;

}

Why ? and how to fix it?
I'm curious as to what you're trying to do that needs such ugly
constructs?

Mar 14 '07 #4

P: n/a
On Mar 14, 11:05 am, "santosh" <santosh....@gmail.comwrote:
cmk...@hotmail.com wrote:
Hi
I am using gcc
void peter(){
int x;
LABEL1:
int y; <--------------- Error

I think a label must precede on or more statements. int y; isn't one.
Correct.

In C++, by the way, a declaration is a statement, so there it would be
valid. It would have been nice if C99 copied C++'s rules regarding
declarations after other statements more completely.
}
But this is ok:
void peter(){
int x;
LABEL1:
; <---- Add an empty line here
int y;
}
Why ? and how to fix it?

I'm curious as to what you're trying to do that needs such ugly
constructs?
I've run into this problem myself with case labels:

switch (i)
{
case 0:
fun1();
break;

case 1:
int j = fun2(i);
if (i == j)
error();
break;
}

Some ways of fixing it:

case 1:
{
int j = fun2(i);
if (i == j)
error();
break;
}

or

int j;
case 1:
j = fun2(i);
if (i == j)
error();
break;

or basically from the original post:

case 1: {}
int j = fun2(i);
if (i == j)
error();
break;

Mar 14 '07 #5

P: n/a
cm****@hotmail.com wrote:
Hi
I am using gcc

void peter(){
int x;
LABEL1:
int y; <--------------- Error
}

Why ? and how to fix it?
Why? Because you have declared a variable other than at the beginning
of a block. You are either using a version of gcc that does not support
the C99 changes to C, or invoking one that does so that it does not
recognize those changes. If you want to continue using gcc in that way,
simply introduce a block:

void peter(){
int x;
LABEL1:
{
int y;
}
}

Of course, y is available only within that block.
Mar 14 '07 #6

P: n/a
Martin Ambuhl wrote:
cm****@hotmail.com wrote:
Hi
I am using gcc

void peter(){
int x;
LABEL1:
int y; <--------------- Error
}

Why ? and how to fix it?

Why? Because you have declared a variable other than at the beginning
of a block. You are either using a version of gcc that does not support
the C99 changes to C,
C99 does not allow a declaration to immediately follow a label.

Mar 14 '07 #7

P: n/a
"Harald van Dijk" <tr*****@gmail.comwrites:
C99 does not allow a declaration to immediately follow a label.
Hmm, that's interesting. I can't find any mention of reasoning
for that in the Rationale, but it's implied by the grammar.

Any idea why?
--
Bite me! said C.
Mar 14 '07 #8

P: n/a
Ben Pfaff <bl*@cs.stanford.eduwrites:
"Harald van Dijk" <tr*****@gmail.comwrites:
>C99 does not allow a declaration to immediately follow a label.

Hmm, that's interesting. I can't find any mention of reasoning
for that in the Rationale, but it's implied by the grammar.

Any idea why?
A label must be followed by a statement. Why would you want to branch
to a declaration?

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Mar 14 '07 #9

P: n/a
Keith Thompson <ks***@mib.orgwrites:
Ben Pfaff <bl*@cs.stanford.eduwrites:
>"Harald van Dijk" <tr*****@gmail.comwrites:
>>C99 does not allow a declaration to immediately follow a label.

Hmm, that's interesting. I can't find any mention of reasoning
for that in the Rationale, but it's implied by the grammar.

Any idea why?

A label must be followed by a statement. Why would you want to branch
to a declaration?
Because you want the variable being declared to be reinitialized?
The following is valid if you add an empty statement after the
label:

label:
int a = func();
...
goto label;
--
int main(void){char p[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuv wxyz.\
\n",*q="kl BIcNBFr.NKEzjwCIxNJC";int i=sizeof p/2;char *strchr();int putchar(\
);while(*q){i+=strchr(p,*q++)-p;if(i>=(int)sizeof p)i-=sizeof p-1;putchar(p[i]\
);}return 0;}
Mar 14 '07 #10

P: n/a
>"Harald van Dijk" <tr*****@gmail.comwrites:
>C99 does not allow a declaration to immediately follow a label.
In article <87************@blp.benpfaff.org>
Ben Pfaff <bl*@cs.stanford.eduwrote:
>Hmm, that's interesting. I can't find any mention of reasoning
for that in the Rationale, but it's implied by the grammar.

Any idea why?
Same reason as everything else that is odd or "broken" in C99:
design by committee. :-)
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (4039.22'N, 11150.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
Mar 15 '07 #11

P: n/a
On Mar 14, 2:02 pm, cmk...@hotmail.com wrote:
Hi
I am using gcc

void peter(){
int x;
LABEL1:
int y; <--------------- Error

}

Why ?
very much strict grammar rule. ! ( i think it should not be )
>and how to fix it?
changing the code like you have done,
>
thanks
from Peter (cmk...@hotmail.com)

Mar 15 '07 #12

This discussion thread is closed

Replies have been disabled for this discussion.