Sobsna, as far as I understood, I cannot change a string if I initialized a pointer to it, for example, char* a("dududu"); (an exception is thrown with an access error while writing, for example, *p = 'a'; ). But I can create an array of characters: char a[]("dududu") , but I do not understand how to bring it into dynamic memory through new. The editor writes:

An aggregate object requires initialization using "{...}"

What does this mean (how to fix it) or how else can I change the string?

Exception code:

 char* a("dududu"); char* p(a); *p = 'a'; 
  • And you do not be lazy to copy the code into the question (code section). After all, many may not understand your story without a code, Personally, I either did not understand ... - AR Hovsepyan
  • It is more interesting for me to know what was not clear. But I updated the question - xt1zer
  • I did not write what I wanted, so I deleted it. - AR Hovsepyan
  • Your first line creates an array of type const char * and its address is written to the pointer. Your read only pointer - AR Hovsepyan
  • one
    Code char* a("dududu"); is not valid, since in C ++ it is forbidden to implicitly cast an array of char const to a pointer to a char , this is apparently your gcc extension. - VTT

1 answer 1

You can make a copy of the string on the stack like this:

 char str[] = "abcd" ; str[1]= 'x' ; 

or in a heap like this:

 # include <string.h> char const * s = "abcd" ; int l = strlen(s) ; char * str = new char [l+1]; memcpy(str,s,l+1); str[1]= 'x' ;