Make you no longer afraid of pointer 0-complex type description

Source: Internet
Author: User

 

To understand pointers, there may be more or less complex types. So I will first introduce how to fully understand a complex type. To understand complex types, there will be many operators in a type, they also have priorities, just like normal expressions. Their priorities are the same as their operational priorities. So I have summarized their principles:

 

Starting from the variable name, it is analyzed step by step based on the combination of operator priorities.

 

Next, let's start from a simple type and analyze it slowly:

 

Int p; // This is a common integer variable.

 

Int * p; // start with P and combine with * First. Therefore, 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]; // start with P and combine it with [] First, indicating that P is an array and then combined with int, indicating that the elements in the array are integer, therefore, P is an array composed of integer data.

 

Int * p [3]; // starting from P, it is first combined with []. Because the priority is higher than *, P is an array and then combined, it means that the elements in the array are pointer types, and then combined with int, it means that the type of the content pointed to by the pointer is integer, so P is an array composed of pointers that return integer data.

 

Int (* p) [3]; // start from P and combine with * First, indicating that P is a pointer and then combine with [] (and "() "This step can be ignored, just to change the priority), it means that the pointer points to an array, and then combined with int, it means that the elements in the array are integer. therefore, P is a pointer to an array composed of integer data.

 

Int ** p; // starting with P, it is first combined with *, that is, P is a pointer, and then combined with *, indicating that the element pointed to by the pointer is a pointer, then combined with int, it indicates that the pointer points to an integer data element. 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 is then combined with an external int, indicating that the return value of the function is an integer data.

 

Int (* p) (int); // starting from P, it is first combined with the pointer, indicating that P is a pointer and then combined, it means that the pointer points to a function, and then combines it with the int in (), indicating that the function has an int-type parameter, and is then combined with the int at the outermost layer, it indicates that the return type of the function is integer, so P is a pointer to a function with an integer parameter and the return type is integer.

 

Int * (* p (int) [3]; // It can be skipped first. It is too complicated to read this type. starting from P, P is first combined with (), indicating that P is a function and then enters (). Combining with int indicates that the function has an integer variable parameter, then it is combined with the outer *, indicating 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, then it is combined with *, indicating 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 is almost the same here, so we have so many tasks and understood these types. Other types are also for us, but we generally do not use too complex types, this will greatly reduce the readability of the program. Please use it with caution. The above several types are enough for us to use. (Is that the end? Of course not. I will finish the following one after another)

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.