The question itself begs an obvious answer. In any case, here's a snippet of my code...
switch(cSet)...
case 8:{ //Special Characters
finalSet = special;
char* charSet = new char[special.size() + 1];
charSet[special.size()] = 0; //Append null terminator
memcpy(charSet, special.c_str(), special.size());
break;
}
case 9:{ //Alphnumeric and Special character
finalSet = all;
char* charSet = new char[all.size() + 1];
charSet[all.size()] = 0; //Append null terminator
memcpy(charSet, all.c_str(), all.size());
break;
}
...
Note that finalSet
is of type std::string
. I'm needing to save it as a character array. After this statement, I call charSet
outside of the switch statement:
for(int i = 0; charSet; i++)
printf("%s", charSet[i]);
Now, it is obvious that switch statements are conditional, so a variable may not always be declared. So Visual Studio 2012 is throwing the error "charSet
is undefined." The way I have my switch statement structured, though, charSet
will always be defined, or the program will exit in the default
case.
To remedy this issue, I attempted to declare charSet
outside the scope of the switch statement. When I do this, however, for some reason the compiler throws a read access error.
I'm curious as to how I can fix this issue.
Any constructive input is appreciated.
Error code when declaring outside of switch statement:
`Unhandled exception at 0x0F6616B3 (msvcr110d.dll) in cuda_comb.exe: 0xC0000005: Access violation reading location 0x00000061.`