C language declaration _ Is it a pointer? Variable? Or a function?

Source: Internet
Author: User

In C, do you have a clear idea about the type of a statement?

It must be a variable or function. But you need to be more specific:

1. variables are classified into: Non-pointer type variables, pointer type variables pointing to variables (This pointer variable may be a non-pointer variable or a pointer variable.), Pointing to the pointer type of the function variable

2. Functions: functions that return non-pointer types and pointer Variables

 

Therefore, in the C language, you need to clearly know which of the five types described above is a declaration.How to analyze and what to analyze.

The following are several statements. analyze them first.

---------------------------------------------------------------

Int P;

Int * P;

Int P [3];

Int * P [3];

INT (* P) [3];

Int ** P;

Int P (INT );

INT (* p) (INT );

Int * (* P (INT) [3];

Int * (* PTR) [4];

---------------------------------------------------------------

How should we analyze it?

The compiler must parse these statements according to certain specifications:

A declares that it reads data starting from its name and then reads data in order of priority;

The priority of B is from high to low:

B .1 The Section enclosed in parentheses in the statement

B .2 suffix operator;

Parentheses () indicate that this is a function, and

Square brackets [] indicate that this is an array.

B .3 prefix operator; asterisk * indicates "pointer ".

C. If the const and (or) Volatile keywords follow the type specifiers (such as int and long), it acts on the type specifiers. In other cases, the const and (or) Volatile keywords act on the pointer asterisk next to its left.

According to the above specifications of the red department, we will resolve them one by one.

Int P;

// Here, p is the simplest non-pointer type variable.

 

Int * P;

// Here p is the simplest pointer type variable

 

Int P [3];

// 1. Read P first according to principle

// 2. According to principle B .1, no brackets except the suffix are found

// 3. According to principle B .2, it is found that P has a suffix [3], so this is an array type non-pointer type variable.

// 4. Combined with the int type, P is an array variable of the int type.

 

Int * P [3];

// 1. Read P first according to principle

// 2. According to principle B .1, no brackets except the suffix are found

// 3. According to principle B .2, the suffix [3] is found, indicating that P is not a pointer type variable, but a non-pointer type variable (array variable)

// 4. What type of array will you consider?

// Replace P [3] with Q and declare it as int * q, which means Q is an int pointer, the array type of P [3] is int pointer type, that is, the element of the array is int type pointer.

// This is the pointer array.

 

INT (* P) [3];

// 1. Read P according to principle

// 2. According to principle B .1, P is a pointer. What type does P point? If * P is replaced by Q, it is declared as int Q [3].

// 3. According to principle B .2, if Q has a suffix [3], then Q and [3] are combined with int, which indicates that Q is an array. Therefore, the p Pointer Points to an array of the int type.

// This is the array pointer.

 

Int ** P;

// 1. Read P according to principle

// 2. According to principle B .1, no brackets except the suffix are found

// 3. According to principle B .2, P has no parentheses or brackets.

// 4. P is directly combined with the first * above, indicating that p is a pointer. What does this pointer point? Similarly, instead of int * q; q is a pointer of the int type, so the P Pointer Points to a pointer of the int type.

 

Int P (INT); // This basic simple function declaration

 

INT (* p) (INT );

// Here it is not detailed. The Compiler first reads P and then combines P with * Because parentheses have a higher priority. That is, P is a pointer, so what does P point, in the same way, it is declared as: int Q (INT)

// So P points to an int Q (INT) function.

 

Int * (* P (INT) [3];

// Read the combination of P, P, and (INT) First, indicating that p is a function whose parameter is of the int type. Then, what type is returned by this function, and replace it with the following statement: int * (* q) [3]; From here we can see that Q and * are combined, so we can see that Q is a // pointer. So what is the Q pointer pointing? Similarly, it is declared as: int * R [3]. From the above, we know that int * R [3] is a pointer array. From this we can see that this is a declaration of a function that returns a pointer type. The returned Pointer Points to a pointer type variable, which points to a pointer array.

 

 

Int * (* PTR) [4];

// PTR is first combined with *, so PTR is a pointer. What does the pointer point to? In the same way, it is declared as: int * Q [4], therefore, the PTR Pointer Points to an array of pointers.

 

Till now, all the five types of declarations mentioned at the beginning have been included.

You .. Don't you understand?

 

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.