char* char[]

Source: Internet
Author: User

If you do not understand, there are also tall wood on the water to explain this:

Here char ch[]= "ABC"; indicates that CH is a one-dimensional array sufficient to hold the initial value of the string and the null character '/0 ', you can change the characters in the array, but the char itself is an immutable constant. char *pch = "ABC"; If the PCH is a pointer, its initial value points to a string constant, and then it can point to another location, but if you try to modify the contents of the string, the result is indeterminate.     ___           ___      ______ch: |abc\0 |    PCH: | ----->  |abc\0 |     ___           ___      ______char Charray[100];charray[i] is equivalent to * (charray+i) and the pointer is different in that the   Charray is not a variable   cannot be assigned to another   in fact I[charray]  is also equivalent to * (Charray+i)

Therefore, it is summarized as follows:

1. char[] p indicates that p is an array pointer, equivalent to a const pointer, which is not allowed to be modified. But the array contents that the pointer points to are allocated on top of the stack and can be modified.

2. Char * PP indicates that PP is a variable pointer that allows modification of it, which can point to other places, such as PP = P is also possible. For *pp = "abc"; in this case, because of compiler optimizations, the ABC is generally stored in the constant area, and then the PP pointer is a local variable, stored in the stack, so in the function return, it is allowed to return the address (actually point to a constant address, string constant area); char[] P is a local variable, and when the function ends, the contents of the array that exist on the stack are destroyed, so the return P address is not allowed.

At the same time, as can be seen from the above example, cout does exist some laws:

1, for numeric pointers such as int *p=new int; Then Cout<<p will only output the value of this pointer, not the content that the pointer points to.
2, for the character pointer into char *p= "SDF F"; then cout<<p will output the data that the pointer points to, i.e. SDF F

So, like & (P+1), because p+1 points to an address, not a pointer, the fetch operation cannot be performed.

&P[1] = &p + 1, which is actually the string content starting from p+1.

Analyze the above program:

*PP = "ABC";

p[] = "ABC";

*PP points to the first character in a string.

cout << pp; The string that returns the start of the PP address: ABC

cout << p; A string that returns the start of the P address: ABC

cout << *p; Returns the first character: a

cout << * (p+1); Returns a second character: b

cout << &p[1];//Returns a string starting with the second character: BC

char* char[]

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.