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
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.
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?
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 = constchar* 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!