0
replace char in string
i'm creating program to make letters lower_case, i have problem with replacing char in string saved in char* - result dumps core https://code.sololearn.com/coCBw45kC4IW/?ref=app problem -> s[i] += 'a' - 'A';
7 Réponses
+ 6
"HELLO WORLD" is stored in read only memory.
char* is an incorrect type to use, it should be const char*.
Attempting to write to read only memory will result in an access violation.
If you used char x[] then the read only memory will be copied to the stack, which is writeable, for x to use.
So no access violation happens there.
If you really want to use a pointer you have to allocate memory from the heap yourself.
+ 1
You could have used a character array to store a string and there is really no need of character pointers because you know things get ugly with pointers.
+ 1
well this solution I've already find out, I'd like to do it with pointer
0
thanks, this is right answer :D
0
ok, now there is second problem - changing the string, any other advice?
0
If you mean resizing, or adding/removing characters, this may help:
https://stackoverflow.com/questions/2937409/resizing-an-array-with-c
0
should i use some flush function?