I’m using C++ Visual Studio .Net 4.0 on Windows 7.0 x64. This happens just on the first loop of the while statement.
int main()
{
char *string = new char[11];
string = "characters\0";
toUppercase(string);
return 0;
}
void toUppercase(char *stringPtr)
{
while(*stringPtr != '\0')
{
if(*stringPtr >= 'a' && *stringPtr <= 'z')
{
*stringPtr = *stringPtr - 32; // this is the culprit
}
++stringPtr;
}
}
I suspect you’re doing something like this:
The problem is
"test"is an array ofconst char, notchar, so cannot be modified. However, due to a terribly stupid deprecated special conversion, a string literal can be treated aschar*anyway.(Your function also fails to test for
toUppercase(0).)