syntax error in switch statement

  • Thread starter Guybrush Threepwood
  • Start date
G

Guybrush Threepwood

This is a simplified example of a problem I'm having:

typedef struct node {
int a;
int b;
} NODE;

Somewhere in my code following lines can be found:

switch(op) {
case someLabel:
NODE *n = node(1, 2);
break;
default:
break;
}

Now when I compile this code with gcc version 3.4.6, I get an error:
error: syntax error before '*' token
error: `n' undeclared (first use in this function)
error: (Each undeclared identifier is reported only once
error: for each function it appears in.)

This has to do with the fact the declaration of *n is the first line of
the case. If I put, for example, an fprintf before the declaration, the
code compiles fine.
 
E

Eric Sosman

Guybrush said:
This is a simplified example of a problem I'm having:

typedef struct node {
int a;
int b;
} NODE;

Somewhere in my code following lines can be found:

switch(op) {
case someLabel:
NODE *n = node(1, 2);
break;
default:
break;
}

Now when I compile this code with gcc version 3.4.6, I get an error:
error: syntax error before '*' token
error: `n' undeclared (first use in this function)
error: (Each undeclared identifier is reported only once
error: for each function it appears in.)

This has to do with the fact the declaration of *n is the first line of
the case. If I put, for example, an fprintf before the declaration, the
code compiles fine.

A label must be followed by a statement. A declaration
is not a statement. Possible fixes

case someLabel:
; /* an empty statement */
NODE *n = node(1, 2);
break;

case someLabel:
/* a compound statement: */
{
NODE *n = node(1, 2);
break;
}
 
T

Thad Smith

Eric said:
A label must be followed by a statement. A declaration
is not a statement. Possible fixes

case someLabel:
; /* an empty statement */
NODE *n = node(1, 2);
break;

While this works for C99, its worth noting that most C compilers don't
support C99. I recommend not using this if portability to C90 compilers
is important.
 
R

Richard Bos

Thad Smith said:
While this works for C99, its worth noting that most C compilers don't
support C99. I recommend not using this if portability to C90 compilers
is important.

Yes; a portable fix is

switch(op) {
NODE *n;
case someLabel:
n = node(1, 2);
break;
default:
break;
}

I must say I'm not sure whether I like that, but it should work.

Richard
 
I

Ian Collins

Richard said:
Yes; a portable fix is

switch(op) {
NODE *n;
case someLabel:
n = node(1, 2);
break;
default:
break;
}
Or add a compound statement:

switch(op) {
case someLabel:
{
NODE* n = node(1, 2);
}
break;
default:
break;
}

Better still, call a function!
 
F

Flash Gordon

Richard Bos wrote, On 08/06/07 07:34:
Yes; a portable fix is

switch(op) {
NODE *n;
case someLabel:
n = node(1, 2);
break;
default:
break;
}

I must say I'm not sure whether I like that, but it should work.

I would do
switch(op) {
case someLabel:
{
NODE *n = node(1, 2);
break;
}
default:
break;
}

Of course, it only makes sense if there is more code in that case making
use of n.
 
R

Richard Tobin

Richard Bos said:
Yes; a portable fix is

switch(op) {
NODE *n;
case someLabel:
n = node(1, 2);
break;
default:
break;
}

I must say I'm not sure whether I like that, but it should work.

I see no strong reason to object to it, but bear in mind that it won't
work to use an initialiser.

-- Richard
 
G

Guybrush Threepwood

Or add a compound statement:

switch(op) {
case someLabel:
{
NODE* n = node(1, 2);
}
break;
default:
break;
}

Better still, call a function!

Ok, thanks for the replies everyone.
 
C

Christopher Benson-Manica

Richard Bos said:
switch(op) {
NODE *n;
case someLabel:
n = node(1, 2);
break;
default:
break;
}
I must say I'm not sure whether I like that, but it should work.

My personal opinion is that it may induce a sleepy programmer to
attempt to use an uninitialized n in one of the switch cases. It's
not quite at the level of handing the maintainer a gun for his foot,
but placing a "SHOOT ME" sign on a foot is still a bad idea :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,954
Messages
2,570,116
Members
46,704
Latest member
BernadineF

Latest Threads

Top