How should we understand complicated pointer structures?

Source: Internet
Author: User
Tags array definition

1. array Definition

An array consists of the type, name, [], and length of an array element.

 
IntA [10] = {0};//Explicit array length: 10IntB [] = {1,2,3};//Implicit array length 3

2. pointer Definition

C ++ uses the * symbol to declare an identifier as a pointer.

Int* P1, * P2;//P1 and P2 point to an intString* Pstring;//Pstring points to a string

 

3. function pointer Definition

Function pointers consist of identifiers, return values, and parameter lists

 
Void(* Funcptr )(Int,Int);

When reading such complex statements, the general method is to start from the middle and expand to both sides in sequence.

Starting from the center, it refers to the variable name, which is also funcptr in this example. Search for the nearest element (none) on the right of the variable, then go to the left (*, indicating that funcptr is a pointer), and then right (INT, INT), parameter list ), then the left side (void, return value ). Therefore, funcptr is a pointer to a function of the void type that points to the parameter list as (INT, INT.

Here are some complex examples:

Void* (* FP1 )(Int))[10];

FP1 is near the right side, and * on the left side. Therefore, FP1 is a pointer. The parameter list (INT) on the right side of FP1 indicates that FP1 is a function pointer and the function parameter list is (INT ), therefore, the rest should be the return value of the function. If fp_ret is used to simplify the process, it can be expressed:

 
Void* (* Fp_ret )[10];

Fp_ret is a pointer pointing to an array with a length of 10. The element type in the array is void *. Therefore, in combination, FP1 is a function pointer that points to a function that has an int type parameter and returns a pointer, this pointer points to an array with 10 void * elements.

 
TypedefDouble(* Fp3 )())[10]) (); Fp3;

Fp3 is a function pointer. It points to an empty function parameter. The returned value is a pointer pointing to an array with a length of 10, an array element is a function pointer with a NULL return value of the double type in the parameter list.

How should we understand complicated pointer structures?

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.