"Go" in-depth understanding of const Char*p,char Const*p,char *const p,const char **p,char const**p,char *const*p,char**const P

Source: Internet
Author: User

  

One, possible combinations:

(1) Const CHAR*P

(2) Char Const*p

(3) Char *const p
(4) const char **P

(5) Char Const**p

(6) Char *const *p

(7) Char **const p

Of course, in (5), (6), (7) and then insert a const in a number of cases, but the analysis of the above 7, the other can be analogy!

Second, understand the key to help remember:

1, the key to see the const modification who.

2, because there is no const * operation , if there is a const * form, Then the const is actually decorated with the front .

For example: Char Const*p, because there is no const* operation, the const is actually decorated with the preceding char, so char const*p is equivalent to the const char*p. That is to say, (1) and (2) are equivalent in the above 7 cases. Similarly, (4) and (5) are equivalent. In (6), because there is no const* operation, the const actually modifies the preceding char*, but cannot be converted to a const (char *) *p at the time of definition, since the definition is "()" is the representation function.

Iii. in-depth understanding of 7 combinations

  The space that is opened up by the program at execution time is in memory (RAM), while the memory unit in RAM is readable and writable; The pointer is just a tool to specify or locate the data to be manipulated, just to read and write the working pointer of the memory unit in RAM. If the pointer is not qualified, a pointer in the program can point to any location in RAM (except for system sensitive areas, such as the area of the operating system kernel ) and read and write to the memory unit it points to (determined by the ram's readable writable property) The readable writable properties of memory units in RAM are not changed by the qualification of the working pointers (see 4th below), and all of the const qualifiers for pointers are plainly confined to the read-write permission (including read-write location) of the pointer.


(1)Char *p:p is a working pointer that can be used to read and write to any location (non-system sensitive area), one byte at a time (char takes one byte).

Note: Special case: const char wang[]={"Wang"}; char *p; P=wang; it's wrong. So p in Char *p cannot point to a constant variable
(2)const char*p or char const *P (because there is no const*p operation, so the const modifier is the preceding char): "Read-only" operations can be performed on any location (non-system sensitive area). ("Read Only" is the content that is qualified relative to char *p)


(3)char *const p (const-Modified P): Read and write only to " One fixed location " and must be initialized when p is defined (because "p=" cannot be performed later.) , so it cannot be initialized later, so it can only be initialized at the time of definition. ("A fixed position" is the content defined relative to char *p)


The above 3 points can be summarized as follows: The pointer p in Char *p is usually a "magnum" working Pointer, while (2) and (3) add some specific restrictions on the basis of (1), which are not required in the program, but to prevent the programmer from being careless and counterproductive.
Also, be aware that "every memory space can have a name, and that the contents of each block of memory are variable (unless limited)". For example, the function defines the char s[]= "Hello", in fact, in the process of the stack memory opened 6 variables a total of 6 bytes of space, where the names of 6 character variables are: s1[0], s1[1], s1[2], s1[3], s1[4], s1[5] (content is ' + ')

{

To be verified: There is also a 4-byte pointer variable s. However, S is "limited", is a char *const type, which is said earlier (3) This case, S has been pointing to s[0], ie (*s==s[0]== ' h '), can be *s= ' k ' to change the value of S point to s[0], but cannot execute (char *h= " AAA "; s=h;) to assign a value to s in addition.

}

(4) the above (2) and (3) only P is limited, no and can not limit the space pointed to P, for "char s[]=" Hello ", const char*p=s;" Although not through * (p+i) = ' x ' or p[i]= ' x ' To modify the value of the array element s[0]~s[4], but can modify the value of the original array element by means of * (s+i) = ' x ' or s[i]= ' x '--ram the readable writable property of the memory unit is not changed by the qualification of the working pointer, but only by its own qualification. such as the const char c= ' A ', C is the name of a memory cell (8 bytes) in RAM, the contents of which are only readable and not writable.

(5)const char **p or char const**p: involves two pointers p and *p. Because of the const modifier char, there is no qualification for pointer p, and the pointer *p is limited to the above case (2).

(6)char *const *p: involves two pointers p and *p. Because the const modifier precedes the char*, that is, the content pointed to by P *p is qualified (also belongs to the preceding case (2)). For *p, because it cannot be assigned by "*p= ...", it belongs to the qualification of the preceding case (3).

(7)char **const P: involves two pointers p and *p,const modifier p, the upper case (3) is limited to p, and there is no qualification for *p.

Iv. type compatibility issues with Char **p, const char **P

1. Questions

Char *p1;const *p2=p1;//Legal

Char **p1;const char**p2=p1;//is not legal, there will be a warning warning:initialization from incompatible pointer type

Char **p1;char const**p2=p1;//is not legal, there will be a warning warning:initialization from incompatible pointer type

char**p1;char*const*p2=p1;//Legal

2. Judging rules

Explicit const-Modified objects! For pointer P1, and P2, to make the p2=p1 set up, it can be read:

"P1 is a pointer to the X type, and P2 is a pointer to the X type with const qualification." as long as the X type is the same, it is legal.

Char *p1;const *p2=p1;//Legal: P1 is a pointer to a (char) type, and P2 is a pointer to the (char) type with const qualification.

Char **p1;const char**p2=p1;//is illegal: P1 is a pointer to a (char*) type, and P2 is a pointer to ((const char) *) type.

Char **p1;char const**p2=p1;//illegal;

char**p1;char*const*p2=p1;//Legal: P1 is a pointer to the (char *) type, and P2 is a pointer to the (char*) type with const qualification.

V. Other

1. Uniform reading of single or double-layered pointers containing const:

"P is a pointer that is a [" const-qualified] Pointer to a "]X type with const-qualified".

L For example: const char* Const *P is said to be: P is a const-qualified pointer to a const-qualified (char*) type.

2, the definition of the const-modified object is determined, but not in the definition of parentheses, or the definition with "()" in the expression of the function type is confused! Therefore, the definition cannot be written (char *) const *P or (const char) **p.

"Go" in-depth understanding of const Char*p,char Const*p,char *const p,const char **p,char const**p,char *const*p,char**const P

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.