473,396 Members | 1,990 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

syntax error in switch statement

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.

--
"Don't worry about people stealing your ideas. If your ideas are any
good, you'll have to ram them down people's throats."
-- Howard Aiken

Jun 8 '07 #1
8 4428
Guybrush Threepwood wrote:
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;
}

--
Eric Sosman
es*****@acm-dot-org.invalid
Jun 8 '07 #2
Eric Sosman wrote:
Guybrush Threepwood wrote:
>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;
}

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.

--
Thad
Jun 8 '07 #3
Thad Smith <Th*******@acm.orgwrote:
Eric Sosman wrote:
Guybrush Threepwood wrote:
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;
}
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.
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
Jun 8 '07 #4
Richard Bos wrote:
Thad Smith <Th*******@acm.orgwrote:
>Eric Sosman wrote:
>>Guybrush Threepwood wrote:

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;
}
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.

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!

--
Ian Collins.
Jun 8 '07 #5
Richard Bos wrote, On 08/06/07 07:34:
Thad Smith <Th*******@acm.orgwrote:
>Eric Sosman wrote:
>>Guybrush Threepwood wrote:

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;
}
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.

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.
--
Flash Gordon
Jun 8 '07 #6
In article <46*****************@news.xs4all.nl>,
Richard Bos <rl*@hoekstra-uitgeverij.nlwrote:
>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
--
"Consideration shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
Jun 8 '07 #7
On Fri, 08 Jun 2007 19:10:10 +1200, Ian Collins wrote:
Richard Bos wrote:
>Thad Smith <Th*******@acm.orgwrote:
>>Eric Sosman wrote:
Guybrush Threepwood wrote:
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.

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!
Ok, thanks for the replies everyone.

--
"Don't worry about people stealing your ideas. If your ideas are any
good, you'll have to ram them down people's throats."
-- Howard Aiken

Jun 8 '07 #8
Richard Bos <rl*@hoekstra-uitgeverij.nlwrote:
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 :-)

--
C. Benson Manica | I *should* know what I'm talking about - if I
cbmanica(at)gmail.com | don't, I need to know. Flames welcome.
Jun 8 '07 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Jeff Duffy | last post by:
Hi all. I've been wondering why python itself doesn't provide a switch to check a file for valid syntax. I know that you can currently call python -c "import py_compile;...
1
by: Donald Canton | last post by:
Hi, I'm using Bjarne's book to learn C++ and am stuck on the Calc program in Section 6. Everything works fine except when I try to use istringstream to parse a token from the command line. I...
9
by: cousaert | last post by:
Newsgroups: alt.comp.lang.learn.c-c++ Date: Fri, 27 Aug 2004 12:36:11 +0200 Lines: 36 User-Agent: KNode/0.7.6 MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii...
12
by: ColinWard | last post by:
Hi. I am trying to run the following code when the user clicks a button, but I am getting a syntax error in the SQL. I have a feeling it has to do with brackets. Can anyone help? here is the...
177
by: C# Learner | last post by:
Why is C syntax so uneasy on the eye? In its day, was it _really_ designed by snobby programmers to scare away potential "n00bs"? If so, and after 50+ years of programming research, why are...
6
by: Christopher Benson-Manica | last post by:
I presume that putting the default case of a switch statement before the others is legal, as in the following skeleton program: #include <stdio.h> int main( int argc, char *argv ) { switch(...
7
by: kosta | last post by:
hello! one of my forms communicates with a database, and is supposed to add a row to a table using an Insert statement... however, I get a 'oledb - syntax error' exception... I have double...
12
by: Andrew Ducker | last post by:
And no, this isn't a complaint about break - I'm very happy to make things explicit. However, why isn't the format something like: switch(myVariable) { case 1: { //Do Something
6
by: redashley40 | last post by:
This is my first attempt in SQL and PreparedStatement I have add the PreparedStatement and I'm not to sure if I'm doing it correctly. When I do a test run on Choose 1 ,or 2 I get this error. Error...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.