Use of string and char* conversion _c language

Source: Internet
Author: User

Copy Code code as follows:

String--> const Char

String str2ch;

Str2ch.c_str ();

//=============================

String--> char *

Convert first to const char and 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 turn string


Mans strcpy
Copy Code code as follows:

#include <string.h>

Char *strcpy (char *dest, const char *SRC);


Attached: pointer constants, constant pointers

What is a pointer constant? A pointer constant is a constant of pointer type.
Example: Char *const name1= "John";
name1= "ABC"; Error, name1 pointer, cannot change, a pointer type variable, storing address, so cannot assign ' ABC ' address to name1
char * name2= name1; OK

What is a constant pointer? A constant pointer is a pointer to a constant, and the value of the pointer can be changed, and the contents of the address that the pointer refers to are constants that cannot be changed.
Example: const char *name1= "John";
Char s[]= "ABC"; Name1=s; Correct, the address that name1 store can change

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

One word, close to which one can not change!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.