change character in C string - why crashes?

Why this crashes?

1
2
char * stri = " ";
stri[0] = '\0'; // here SIGSEGV - segmentation fault 


It was just a try to change a character in char array using specified position.
Because modifyind a string literal is undefined.

1
2
3
4
5
6
char * stri = " "; //creates cagr pointer stri and makes it point to string literal
//Which possibly is placed in read-only memory
//Outdated conversion: real type of string literals is const char*
//Any modern properly configured compiler should give you an error here

stri[0] = '\0';//Probably trying to modify read only memory 
OK, I see I have mistaken with this: http://tinyurl.com/osqqv8n
1
2
char stri[] = " ";
stri[0] = '-';


Useful link (for me):
http://stackoverflow.com/questions/27468280/can-we-change-string-characters-pointed-by-pointers
The difference between string literals and char arrays greatly explained here:
http://stackoverflow.com/questions/1704407/what-is-the-difference-between-char-s-and-char-s-in-c

And this code is correct? It does not crash.
1
2
3
char * stri = (char *) malloc(10);
strcpy(stri, " ; ");
stri[0] = '\0';


However what surprised me is that after \0 is assigned to stri[0], code::blocks shows 0x4c2470 "" , not 0x4c2470 "\0;0" ... Did I just completely overwritten the buffer? I think it's not possible, the buffer should have size 10 bytes.

Edit:
Yeah, it works. Just the code::block does not show the rest of buffer.

1
2
3
char * stri = (char *) malloc(10);
strcpy(stri, " ; ");
stri[0] = '-'; // works now 


But I can set Properties in the watch panel to check "Watch as array" and now I see it is ok even with \0 character in first position.
Last edited on
Just the code::block does not show the rest of buffer.
Why would it? It looks like C-string, so it threats it like one. If you need to see rest of the buffer, look at memory dump.

Also avoid malloc in C++. Use new instead.
In this case:

1
2
3
settings->arguments = (char*) malloc(strlen(argv[i]));
settings->arguments[0] = '\0'; // Works
strcpy(settings->arguments, myFnc(argv[i]));


Is the following still string literal?
1
2
3
char * myFnc(char *str) { // i think it only created new pointer to the same buffer above
str[0]=" "; // warning: this make integer from pointer
str[1]='\0';


How to access the string *str in myFnc so that I could change it?
Last edited on
i think it only created new pointer to the same buffer above
Yes, it does not copy strings. If original was string literal then function argument is a pointer to string literal too. If it was pointer to allocated bufffer, it would be same here

warning: this make integer from pointer
str has type char*. str[0] has type char. " " is a string literal and has a type of const char*. char = const char* do you see type mismatch here? Did you intend to use single qotes instead of double?

1) Do not use malloc. Use new.
2) Consider getting rid of c-strings altogether and use std::string
3) Configure your compiler properly. char* str = "Hello!"; should be an error!
Thanks!
I did not know there is such huge difference in the brackets!

So "" makes string literals const char*
and '' makes single character.

That's quite difference.
Last edited on
Topic archived. No new replies allowed.