C + + string and pointer all the content is so much.

Source: Internet
Author: User

1. Define a string array and initialize it, and then output the string within it.

#include <iostream>

using namespace Std;
int main ()
{
Char str[]= "I love China";
cout<<str<<endl;
return 0;
}

STR is the character array name, which represents the address of the first element of the character array, starting with the character that the Str points to, outputting characters one after the other until ' \ n ' is encountered.

2. Use string variables to store strings.

Define a string and initialize it, and then output the string within it.

#include <iostream>
#include <string>
using namespace Std;
int main ()
{
String Str= "I love China";
cout<<str<<endl;
return 0;
}

3. Point to a string with the character pointer.

Define a character pointer variable and initialize it, and then output the string it points to.

#include <iostream>
using namespace Std;
int main ()
{
Char *str= "I love China";
cout<<str<<endl;
return 0;
}

The character pointer variable str initialization, is actually the address of the 1th element of the string to STR, the system output, first output str point to the first character data, and then the STR automatically add 1, so that it points to the next character, and then output a character. Until the end of the string is encountered. Note that in memory, the end of the string is automatically added a '/', so at the output you can determine where the string ends.

4. Use the pointer to copy the string.

#include <iostream>
using namespace Std;
int main () {
Char str1[]= "I love China";
Char str2[20];
Char *p1;
Char *p2;
P1=STR1;
P2=STR2;
for (; *p1!= ';p 1++,p2++)
*P2=*P1;
*p2= ' + ';
P1=STR1;
P2=STR2;
cout<< "str1 is:" <<p1<<endl;
cout<< "str2 is:" <<p2<<endl;
return 0;
}

C + + string and pointer all the content is so much.

Related Article

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.