INFO: Initializing in a Loop Within a Case StatementLast reviewed: September 8, 1997Article ID: Q86479 |
The information in this article applies to:
SUMMARYIn the products listed above, the following errors can occur when you define a variable in a loop that is within a case statement. The following error occurs when "default" is the next case:
C2361 Initialization of 'x' is skipped by 'default' labelWhen default is not next, the following error appears:
C2360 Initialization of 'x' is skipped by 'case' labelA similar error also occurs when a goto is before the loop:
C2362 Initialization of 'x' is skipped by 'goto label name' MORE INFORMATIONIn the two samples below, it can be seen that the variable is initialized when it is declared. The errors are generated because the scope of the variable, when it is defined and initialized, is in the same scope as the case label or the goto label. Thus, there is a chance that the initialization will not occur. There are ways to ensure that the initialization is performed:
Sample Code 1
#include <stdio.h> /* Compile Options needed: none. */ void main( void ) { int var; switch( var ) { case 1 : for (int i = 0; i < 3; ++i) { printf("In loop"); } break; case 2 : printf("Case 2"); // C2360 break; default : printf("Default"); // C2361 break; } } Sample Code 2
#include <stdio.h> /* Compile options needed: none. */ void main (void) { goto label; int var = 0; label:; // C2362 } Keywords : CPPLngIss kbcode Version : MS-DOS:7.0; WINDOWS:1.0,1.5; WINDOWS NT:1.0,2.0,4.0,5.0 Platform : MS-DOS NT WINDOWS Issue type : kberrmsg kbinfo |
================================================================================
© 1998 Microsoft Corporation. All rights reserved. Terms of Use. |