AndrewBGS
Member
|
simple string operation
I'm trying to do something I was doing in the 9th grade with no problem. And here I can't seem to make it work.
I want to alter a char * variable piece by piece. Basically I want to build up the variable at different parts of code. But leaving that aside, this is what I'm doing:
char *LEVEL="01.dat";
LEVEL[0]='0';
As simple as that. No compile errors, but the precise row < LEVEL[0]='0'> is making my program crash. And I have no idea why. I know there are alternatives and I could try using string or something, but I wouldn't want to change a ton of code to adapt to them. I mean this HAS to be doable. So what am I missing here?
|
|
09-16-2013 01:23 PM |
|
Rubeus
Member
|
RE: simple string operation
Don't you want:
char LEVEL[] = "01.dat";
LEVEL[0] = '0';
?
If you do it how you are, you are trying to modify a pointer to a string literal....
|
|
09-17-2013 05:57 AM |
|
Esenthel
Administrator
|
RE: simple string operation
Code:
char *LEVEL="01.dat";
LEVEL[0]='0';
The code is correct, except one thing: the string here is in read-only memory address.
The solution is to use Rubeus' code, which creates a local copy of the string.
|
|
09-17-2013 06:26 AM |
|
AndrewBGS
Member
|
RE: simple string operation
Thank you very much. It's hard for me to understand what I'm doing wrong when there is no compilation error just a crash. Thank you very much!
|
|
09-17-2013 08:09 PM |
|