Char*,const the conversion problem between char*,char[],string.

Source: Internet
Author: User

#include <iostream>
#include <stdlib.h>
#include <string>


using namespace Std;
int main ()
{
string and const char* Mutual transfer
Const char* A;
String str_1 = "My name is Pang";
A = Str_1.c_str ();
cout<<a<<endl;


Const char* Turn string
Const char* a_1 = "Hello string";
String str_2 (a_1);
cout<<str_2<<endl;


String Turn char*
String str_3 = "string to char*";
char* a_2;
int lenofstr = Str_3.length ();
a_2 = new CHAR[LENOFSTR];
strcpy (A_2,str_3.c_str ());
cout<<a_2<<endl;


String Transfer char* Method 2
String str_31 = "string to char* function 2";
char* a_31;
a_31 = const_cast<char*> (Str_31.c_str ());
cout<< "The function of String to char*" << a_31<<endl;


char* Turn string
String str_4;
char* a_3 = "char * to String";
Str_4 = A_3;
cout<<str_4<<endl;


Const char* Turn char*
Const char* a_5 = "Const char* to char*";
char* a_6 = new char[100];
strcpy (a_6,a_5);
cout<<a_6<<endl;
Delete A_6;


The strcpy () function is to copy the contents of the A_5 to A_6, but the copy is cut off when the sentence is encountered with the/terminating character as follows:
Const char* a_7 = "Const char* to char*";
char* a_8 = new char[100];
strcpy (a_8,a_7);
The cout<<a_8<<endl;//output A_8 is only copied to the const char*

Using Const_cast can also be implemented
Const char* a_71 = "Const char* to char*";
String str_71 (a_71);
A_8 = const_cast<char*> (Str_71.c_str ());
cout<<a_8<<endl;


char* go to const char*
char* a_9 = "A_9 char* to const char*";
Const char* A_10 = A_9;
cout<<a_10<<endl;


Char[] Turn string
Char a_11[10]= "123456";
string str_11 = A_11;
cout<<str_11<<endl;


String to char[]
String Str_12 = "123321";
Char a_12[12];
strcpy (A_12,str_12.c_str ());
cout<<a_12<<endl;


char* Turn char[]
char* a_13 = "\n\n\nchar* to char[]";
Char arr_1[100];

cout<< &a_13<<endl;
cout<< (a_13[0]) <<endl;
cout<< &a_13<<endl;

cout<< &arr_1<<endl;
&arr_1 = *a_13;
strcpy (ARR_1,A_13);
cout<<arr_1<<endl;
Incidentally, the concept of character pointers, when creating a character pointer, memory opened up two space, a space to store char* to char[] This sentence, another to store char* type of a_14,This a_14\
is a character pointer, has its own memory location, its station a memory address, its own value is the first address of the following sentence.
char* a_14 = "char* to char[]";

printf ("%x\n", &a_14);
printf ("%x\n", &a_14[0]);
printf ("%x\n", a_14);
}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Char*,const the conversion problem between char*,char[],string.

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.