C6246

 

The new home for Visual Studio documentation is Visual Studio 2017 Documentation on docs.microsoft.com.

The latest version of this topic can be found at C6246.

warning C6246: Local declaration of <variable> hides declaration of same name in outer scope. Additional Information: See previous declaration at <location>.

This warning indicates that two declarations have the same name at local scope. The name at outer scope is hidden by the declaration at the inner scope. Any intended use of the outer scope declaration will result in the use of local declaration.

The following code generates this warning:

#include <stdlib.h>  
#define UPPER_LIMIT 256  
int DoSomething( );  
  
int f( )  
{  
  int i = DoSomething( );  
  if (i > UPPER_LIMIT)  
  {  
    int i;  
    i = rand( );  
  }  
  return i;  
}  

To correct this warning, use another variable name as shown in the following code:

#include <stdlib.h>  
#define UPPER_LIMIT 256  
int DoSomething( );  
  
int f ( )  
{  
  int i = DoSomething( );  
  if (i > UPPER_LIMIT)  
  {  
    int j = rand( );  
    return j;  
  }  
  else  
  {  
    return i;  
  }  
}  

This warning only identifies a scope overlap.

Show: