Use of string and char* conversions

  • 2020-04-01 21:38:40
  • OfStack


//string --> const char
 string str2ch ; 
str2ch.c_str();
 
 //=============================
 
//string --> char *
//Const char, then char *
   char TargetFile[strlen(TorrentFileNameDown.c_str())];
    strcpy(TargetFile,TorrentFileNameDown.c_str()); //Change type const char to char *
 
 //=============================
 
//char * --> string
//int main(int argc , char *argv[])
   string strCommand_down ;
    strCommand_down.assign(argv[1],strlen(argv[1]) ); //Char array to string

Man strcpy

 #include <string.h>
 char *strcpy(char *dest, const char *src);

Attachment: pointer constant, constant pointer

What is a pointer constant? Pointer constants are constants of pointer types.
Example: char *const name1="John";
      Name1 = "ABC". // error, name1 pointer, cannot be changed, a pointer type variable, is the address, so you cannot assign 'ABC' address to name1
      Char * name2 = name1; / / can

What is a constant pointer? A constant pointer is a pointer to a constant. The value of the pointer can be changed.
Const char *name1="John";
      Char [] s = "ABC"; Name1 = s; // correct, the address of name1 can be changed

      Char * name2 = name1; // no, because name2 and name1 store the same address, if the content of name2 address is changed, then the content of name1 is also changed, then name1 is no longer a pointer to a constant.

In a word, close to which which cannot be changed!


Related articles: