Thread: Label cannot attach to a declaration

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    144

    Label cannot attach to a declaration

    Hi everyone. When I compile the following code using gcc -Wall -Wextra, GCC tells me : "error: a label can only be part of a statement and a declaration is not a statement" for the line after case 2:. I got it to compile by inserting a null statement (ie a semicolon) after the colon in case 2:. Is this correct C99?

    Code:
    switch (variable)
    {
    case 1:
       blah ;
    case 2:
    char *myptr = strstr (str1 , str2);
    ...
    }
    Last edited by Richardcavell; 03-20-2011 at 12:50 AM. Reason: clarity

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Post your actual code, and highlight the segment it's talking about. Cases aren't new code blocks though. If you need to declare a variable there:
    Code:
    case foo:
    {
    
    }
    break;
    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    If you've got declarations specific to cases, you normally write
    Code:
    case 2:
    {
        char *myptr = strstr (str1 , str2);
        ...
    }
    break;
    This also works with C89, where your ; trick would not.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 08-16-2010, 10:00 AM
  2. Quantum Random Bit Generator
    By shawnt in forum C++ Programming
    Replies: 62
    Last Post: 06-18-2008, 10:17 AM
  3. failure to import external C libraries in C++ project
    By nocturna_gr in forum C++ Programming
    Replies: 3
    Last Post: 12-02-2007, 03:49 PM
  4. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  5. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM

Tags for this Thread