syntax error in switch statement

Discussion in 'C Programming' started by Guybrush Threepwood, Jun 8, 2007.

  1. 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
     
    Guybrush Threepwood, Jun 8, 2007
    #1
    1. Advertising

  2. Guybrush Threepwood

    Eric Sosman Guest

    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
    lid
     
    Eric Sosman, Jun 8, 2007
    #2
    1. Advertising

  3. Guybrush Threepwood

    Thad Smith Guest

    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
     
    Thad Smith, Jun 8, 2007
    #3
  4. Guybrush Threepwood

    Richard Bos Guest

    Thad Smith <> wrote:

    > 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
     
    Richard Bos, Jun 8, 2007
    #4
  5. Guybrush Threepwood

    Ian Collins Guest

    Richard Bos wrote:
    > Thad Smith <> wrote:
    >
    >> 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.
     
    Ian Collins, Jun 8, 2007
    #5
  6. Guybrush Threepwood

    Flash Gordon Guest

    Richard Bos wrote, On 08/06/07 07:34:
    > Thad Smith <> wrote:
    >
    >> 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
     
    Flash Gordon, Jun 8, 2007
    #6
  7. In article <4all.nl>,
    Richard Bos <> wrote:

    >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.
     
    Richard Tobin, Jun 8, 2007
    #7
  8. On Fri, 08 Jun 2007 19:10:10 +1200, Ian Collins wrote:

    > Richard Bos wrote:
    >> Thad Smith <> wrote:
    >>
    >>> 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
     
    Guybrush Threepwood, Jun 8, 2007
    #8
  9. Richard Bos <> wrote:

    > 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.
     
    Christopher Benson-Manica, Jun 8, 2007
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    21
    Views:
    1,063
    Giannis Papadopoulos
    Aug 2, 2005
  2. bthumber
    Replies:
    5
    Views:
    423
    Alexey Smirnov
    Jan 29, 2009
  3. Paul Lalli
    Replies:
    2
    Views:
    297
    thundergnat
    Aug 26, 2005
  4. Mark Richards
    Replies:
    3
    Views:
    320
    Tad McClellan
    Nov 18, 2007
  5. Switch Within A Switch

    , Apr 22, 2006, in forum: Javascript
    Replies:
    7
    Views:
    111
    Lasse Reichstein Nielsen
    Apr 22, 2006
Loading...

Share This Page