Edit: I did not notice this when I wrote the original answer (see below). The code you posted is illegal and causes undefined behavior. The offensive line is:
char *x = strchr(x,' ');
Here, x in the strchr call strchr not refer to x defined in the application area, but to x defined earlier on the same line. Therefore, this line is read from an uninitialized variable, which leads to undefined behavior. From the C ++ standard
ยง3.3.2 / 1 [basic.scope.pdecl]
The declaration point for the name immediately after its full declaration (section 8) and before its initializer (if any), except as noted below. [Example:
int x = 12; { int x = x; }
Here the second x is initialized with its own (undefined) value. -end example]
GCC complains if the corresponding line in the example below is changed to
int x = 21 + x;
And duplicating your strchr example on VS2012 generates this warning (in /W1 and above):
warning C4700: uninitialized local variable 'x' used
The original answer follows (not entirely accurate):
There is nothing illegal in the code. You have entered a new scope by adding curly braces, and you are allowed to define variables in the new scope, even if these variable names were previously defined in the application scope.
All references to the variable after the new definition will refer to the local variable instead of the one located in the enclosing area, until the local variable expires. The following code does not generate any warnings about GCC even when compiling with -pedantic -Wall -Wextra
#include <iostream> int main() { int x = 42; { std::cout << x << '\n'; int x = 21; std::cout << x << '\n'; } std::cout << x << '\n'; }
Output:
42 21 42
I do not know if this tool or the static analysis tool will understand. This is legal but not recommended.