Analysis of examples of C + + pointers

Source: Internet
Author: User

See a long time of C, basically is the basic grammar clearance, met a C + + face test, analysis, as a period of time punch.

Code inside the compiler,

1#include <iostream>2 3 using namespacestd;4 5 intMainintargcChar**argv)6 {7     Char*str[]={"Welcome"," to","Boyaa","Shenzhen"};8     Char**p=str+1;9 Tenstr[0]= (*p++) +1; Onestr[1]=* (p+1); Astr[2]=p[1]+3; -cout<<str[2]-str[1]<<Endl; -  thestr[3]=p[0]+ (str[2]-str[1]); -cout<<str[0]<<" "<<str[1]<<" "<<str[2]<<" "<<str[3]<<Endl; -     return 0; -}

The result of the code operation is:

30 (0x0)   0.366continue.

Here is your own simple analysis process, halfway may be a little error (people remind me) ———————— "

First, there are some initialization steps.

i) request an array of strings, which is the address of the string that is stored in the array:
Char *str[]={"Welcome", "to", "Boyaa", "Shenzhen"};
STR is here equivalent to a constant level two pointer.

II) Define a level two pointer char**p=str+1;
Assign P value &str[1]

III) OK, the play is coming back to the string array of Str just mentioned. It's all a series of operations on a string array
str[0]= (*p++) +1;
str[1]=* (p+1);
str[2]=p[1]+3;
cout<<str[2]-str[1]<<endl;

1) First (*p++) +1, wherein the *p++ priority is * (p++), and then first return the value of p to the expression, equivalent to *p,p++;
Then str[0]=*p+1 equivalent to str[1]+1 equivalent to str[1][2], that is, from the second letter to the beginning to the last
Notice that this time the string array changes and becomes
Char *str[]={"O", "to", "Boyaa", "Shenzhen"};

2) Okay, the second one.
str[1]=* (p+1), where * (p+1) equals * (&str[2]+1) equals * (&str[3]) equals str[3], which is
Char *str[]={"O", "Shenzhen", "Boyaa", "Shenzhen"};

3) The third str[2]=p[1]+3, where the p[1]+3 equivalent to str[3]+3 equivalent to "Shenzhen" +3 is equivalent to "Nzhen"
"Shenzhen" is not a string, it is an address. All string constants are addresses.
This time is the "Nzhen" address assigned to STR[3], so there are changes
Char *str[]={"O", "Shenzhen", "Nzhen", "Shenzhen"};

4) Then the final output is the first one.
cout<<str[2]-str[1]<<endl;
It looks like a two string subtraction, and the result is that the first address of the string is subtracted from the value "Shenzhen"-"nzhen" equals the address of S minus N.
The result is 3.

5) The fifth step is to assign value, str[3]=p[0]+ (Str[2]-str[1]);
That is str[3]= "Nzhen" +3= "en", which is an operation of an address
Update STR has
Char *str[]={"O", "Shenzhen", "Nzhen", "en"};

6) Finally the result output
cout<<str[0]<< "<<str[1]<<" "<<str[2]<<" "<<str[3]<<endl;
o shenzhen nzhen en

Analysis of examples of C + + pointers

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.