LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

yet another c99 extension variable declaration 8.5 compiler error

    case UUTPOWER_SET_OUTPUT:
                
        
1573               int iOutput;    <=============   doesn't work  "Unrecognized statement"  
1574                szTemp[1] = '\0';    int iOutput;     <============ works but says it's a re-declaration of the above! 
                szTemp[0] = msg[nSlaveNum].hdr.Info.UUTPowerOutput.szOutputState[0];
                sscanf (szTemp, "%i", &iOutput);
                
        break;
           
      
  
  1573, 9   Unrecognized statement.
  1573, 13   syntax error; found 'identifier' expecting ';'.
  1573, 13   Undeclared identifier 'iOutput'.
  1574, 38   Redeclaration of 'iOutput' previously declared at MasterPipes.c:1573.

0 Kudos
Message 1 of 7
(3,653 Views)

Hello menchar,

Thank you for posting on the NI Discussion Forums.  From examining the code excerpt that you included in your post, it seems that you are attempting to declare the variable “iOutput” in an illegal place in your code.  You cannot declare variables inside of a case statement.  Try moving your declaration of “iOutput” to the beginning of the local function, or to the top of the source file (depending on the desired scope of the variable). 

Chris_G
Sr Test Engineer
Medtronic, Inc.
0 Kudos
Message 2 of 7
(3,621 Views)
I'm using the C99 extension.

That should have worked Smiley Surprised

         switch (n)  {

         case 1:
259      int i = 0;
         strcpy (szPipeName[n], PIPEPOS1);
261      int j = 1;
         strcpy (szDriverPipeName[n], DRIVERPIPE1);
263      int k = 2;
         break;


 MasterPipes.c - 3 errors, 2 warnings
  259, 11   Unrecognized statement.
  259, 15   syntax error; found 'identifier' expecting ';'.
  259, 15   Undeclared identifier 'i'.
  261, 15   Warning: Local 'int j' is not referenced.
  263, 15   Warning: Local 'int k' is not referenced.

0 Kudos
Message 3 of 7
(3,619 Views)
Chris,

Menchar is using an undocumented feature of CVI 8.5, one which has been informally discussed here in the forum, in the past. He's correct, this should have worked. This will be fixed in the next version.

Thanks,
Luis
0 Kudos
Message 4 of 7
(3,613 Views)
Actually, this is technically illegal C. Both ANSI C and the ISO C99 spec allow case/default only as part of a labeled statement:

labeled-statement:
    identifier : statement
    case constant-expression : statement
    default : statement

So, even under C99 rules, a declaration cannot directly follow a case, default, or user-defined label. However, because this is a fairly unintuitive and unnecessary restriction, we'll probably "fix" this anyway and lift it in the next release.

Mert A.
National Instruments
0 Kudos
Message 5 of 7
(3,595 Views)
After reading my H&S syntax, I have to agree, my bad.  No way to get a declaration immediately after a label without using a block (compound statement).  The syntax could have used 

labeled-statement :
    label : declaration-or-statement

but they didn't, they used

label : statement

I'd say don't change it then.

I think I'll try this with the Intel compiler and see what it does.

The H&S discussion of the switch statement would lead you to believe that you can use a declaration right after the case-label.  It's only when you go through the syntax that you see that the lcase-label is used only as part of a labeled statement.

Menchar


Message Edited by menchar on 07-21-2008 05:49 PM
0 Kudos
Message 6 of 7
(3,587 Views)
BTW don't get me wrong - I am using the in-block local declaration all the time, and it's a great addition.

the ability to do this

for (int i = 0; i < iSomeValue; i++) {

 
for (int i = 0; i < iSomeValue; i++) { ... }

}

with the scope nesting without error or confusion is of immense help.

Menchar
0 Kudos
Message 7 of 7
(3,573 Views)