There was this question: Converting char [] to string

In addition to it, I had an additional question about the work of the String constructor (char *). What happens when calling such a constructor - is a separate memory area allocated for the String class into which the contents of the char * are copied?

Or is some sort of reference to the existing char * and the variable char * cannot be formed?

Those. a copy of the string char * gets into the variable of the String class - or does a certain pointer fall into a certain area of ​​memory that needs to be saved?

const char txt[]="Пример"; char * b = &txt; String s(b); 
  • five
    And what is the class String? Is that what you called std::string ? - KoVadim
  • I apologize - I just write for Arduino, there std :: string for some reason was called String - Gayrat Vlasov
  • There is a completely different class. Though the name is similar. - KoVadim
  • Completed the answer about Arduino - German Borisov
  • char * b = &txt; - this is already incorrect. - AnT

1 answer 1

The constructor description std::string says:

from c-string (4) string (const char * s);

Copies the null-terminated character sequence (C-string) pointed by s

those. copied content.

UPD in connection with comments about Arduino

As KoVadim correctly noted, String and std::string are completely different classes.

GitHub has open source. Specifically, the String class is described in the WString.cpp file .

There, just like in std::string constructor copies the data.

  • one
    Did you read my comment carefully? - KoVadim
  • @KoVadim, does your comment contain useful information? - German Borisov
  • On the issue it is not at all clear exactly what class the String has in mind. Therefore, it is impossible to answer. But I assumed that there is std :: string. So yes, my comment contains a lot of useful information - KoVadim
  • Herman, thanks for poking your nose :) I assumed that most likely the designer is copying, but I could not find the confirmation, thanks again :) - Gayrat Vlasov
  • Yeah, actually I was right, And Herman hurried - KoVadim