Constant pointer (pointer to constant) and pointer constant

Source: Internet
Author: User

First, post a document

Int p; // This is a common integer variable int * p; // It starts from P and is first combined with *, so P is a pointer and then combined with int, indicates that the Pointer Points to the int type of content. so P is a pointer to return integer data, int p [3]; // starting from P, it is first combined with [], indicating that P is an array and then combined with int, it indicates that the elements in the array are integer, so P is an array consisting of integer data, int * p [3]; // starts from P and combines with [] first, because the priority is higher than *, P is an array, and then combined with *, it means that the elements in the array are pointer types, and then combined with int, this indicates that the pointer points to an integer type, so P is an array of int (* p) [3] consisting of pointers that return integer data; // start with P and combine with *, indicating that P is a pointer. then combine it with [] (and "()" This step can be ignored, just to change the priority), it means that the content pointed to by the pointer is an array, and then combined with int, the elements in the array are integer. so P is a pointer int ** p to an array composed of integer data; // It starts from P and first combines with *, indicating that P is a pointer, then it is combined with *, indicating that the element pointed to by the pointer is a pointer, and then combined with int, indicating that the element pointed to by the pointer is an integer data. since second-level pointers and more advanced pointers are rarely used in complex types, we will not consider multi-level pointers for more complex types. At most, we will only consider first-level pointers. int p (int); // starts from P. It is first combined with (), indicating that P is a function and then analyzed in, it indicates that the function has an integer variable parameter, and then it is combined with an external int, indicating that the return value of the function is an integer int (* p) (int ); // starting from P, it is first combined with the pointer, indicating that P is a pointer, and then combined with (), indicating that the pointer points to a function, and then () the int combination in indicates that the function has an int-type parameter, and is combined with the outermost int, indicating that the return type of the function is integer, so P is a pointer to a function that has an integer parameter and returns an integer x (* p (int) [3]; // starts with P, combining with () indicates that P is a function, and then entering (). Combining with int indicates that the function has an integer variable parameter, which is then combined with * outside, it indicates that the function returns a pointer, and then goes to the outermost layer. First, it is combined with [], indicating that the returned Pointer Points to an array and then is combined, it indicates that the elements in the array are pointers, and then combined with int, indicating that the Pointer Points to integer data. therefore, P is a function that takes an integer value and returns a pointer variable pointing to an array composed of integer pointer variables. // It's almost the same here, so we have so many tasks and understood these types. Other types are also for us, however, we generally do not use too complex types, which will greatly reduce the readability of the program. Please use them with caution. The above types are enough for us to use.

 

Now we will analyze the special: const char * pStr and char * const pStr. I do not know whether the following statements are correct. If you have better comments, please leave me a message indicating errors or deficiencies. Thank you very much!

After reading the above information, I understand const char * pStr and char * const pStr in this way.

Const char * pStr;

1. First, there is no const keyword in the left combination of pStr, which indicates that pStr is a variable but not a constant

2. Combined with *, it indicates that pStr is a pointer variable.

3. Combined with char, it indicates that pStr is a pointer variable pointing to the character type.

4. Combined with const, it indicates that pStr is a pointer variable pointing to the constant character type (obviously incorrect, pStr should be a pointer variable pointing to the constant string type)

Char * const pStr;

1. First, a const keyword is added to the left of pStr, indicating that pStr is a constant rather than a variable.

2. Combined with *, it indicates that pStr is a pointer constant.

3. Combined with char, it indicates that pStr is a pointer constant pointing to the character type (obviously incorrect, pStr should be a pointer constant pointing to the string type)

 

Constant pointers and pointer constants are really difficult to understand. I used to understand this knowledge point in a rather understandable manner. I had to go through the book whenever I needed it, and often confused it. I understood the above information. The definition of variables can also be described in the opposite way. It is easy to understand.

However, I was confused when I wrote this blog post, so I looked for information again. Link: Workshop. After reading this blog post, I have deepened my understanding of this knowledge point. The previous understanding is at the pointer restriction level. This blog post shows me the Minimum Memory limit level. However, I have a question:"Because noConst *"What does this mean?

Now, I understand const char * pStr and char * const pStr in this way.

Const char * pStr;

1. First, we should break down const char * pStr into const char * And pStr respectively, so pStr is a variable of the const char * type that points both pStr to a constant string. Or pStr stores another memory address in the memory, which stores strings (the data in the memory is readable and writable, except for the sensitive area of the system, such as the operating system kernel region), the const keyword only limits that our programmers can only perform read-only operations on this memory. As for the pStr memory, we can read and write it at will.

Char * const pStr;

1. First, we should break down char * const pStr into char * and const pStr respectively. Then pStr is a constant of the char * type, which points to a string. Or pStr stores another memory address in the memory, which stores strings (the data in the memory is readable and writable, except for the sensitive area of the system, such as the operating system kernel region), the const keyword only limits that our programmers can only perform read-only operations on the pStr memory. We can read and write the memory that stores strings at will.

  

At the end of the article, I couldn't help wondering if I was too stupid. I was not sure whether I understood it correctly. It was too late. After several hours, my mind was a bit confused.

So I ask all of you to express your views and help me understand them. Thank you!

 

Correction:

1:

Const char * pStr = "Hello World"; // pStr is a pointer variable pointing to a constant string.

PStr = "hello"; // pStr can point to another constant string

PStr [0] = 'n'; // Error, Here pStr [0] operates on the character of the constant string

 

2:

Char * const pStr = "Hello World"; // pStr is a pointer constant pointing to a string.

PStr = "Hello"; // Error. Here pStr is a constant, and its value cannot be changed after initialization (bound to "Hello World"), which is equivalent to reference.

PStr [0] = 'n'; // OK. Here pStr [0] operates on string characters

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.