I am wondering if a conditional operator could actually prevent other unrelated code from working. For example below:
typedef char WCHAR_T;
#define STRLEN(x) strlen(x)
if (argc > 2)
{
WCHAR *pFileName = argv[1];
basic_string <WCHAR> strFileName(pFileName, STRLEN(pFileName));
}
In the code above, pFileName, argv[1] and strFileName have nothing to do with the comparison argc > 2. Assuming that the command line arguments are perfectly fine. However, this code would not work with that comparison. Also, even if I change the code to the following format:
typedef char WCHAR_T;
#define STRLEN(x) strlen(x)
{
WCHAR *pFileName = argv[1];
basic_string <WCHAR> strFileName(pFileName, STRLEN(pFileName));
}
Still doesn’t work.
EDIT: By “doesn’t work”, I mean compiler gives error message such as “strFileName” was not declared, which means this declaration didn’t run at all.
I have no idea why the curly braces would have such big impact at the code. When I get rid of the curly braces, the code works like magic…Could anyone explain this please? Thanks.
i guess you’re try to use the variable
strFileNamefrom somewhere outside the braces, which is not possible.the
{and}braces define a block and variables declared inside are only existent inside this block.a workaround would be to declare
strFileNameoutside of the block and assign it’s value from inside