Understanding complex pointer declarations in C ++

Source: Internet
Author: User


Understand the "right-left rule" available for complex statements: Starting from the variable name, the variable name must first go to the right, and then to the left. A parentheses

The reading direction is changed. After the analysis in the brackets is complete, the brackets are displayed, or in the order of right and left.

The entire statement is analyzed. Example:

Int (* func) (int * p );

First, find the variable name func. There is a pair of parentheses and a * sign on the left. This indicates that func is a pointer.

And then jump out of the parentheses, first look at the right side, and then encounter parentheses, which means (* func) is a function, so

Func is a pointer to this type of function, that is, a function pointer. This type of function has an int * type parameter and returns a value.

The type is int.

Int (* func [5]) (int *);

The right side of func is a [] Operator, indicating that func is an array with five elements; the left side of func is *, indicating

The element of func is a pointer (note that * is not modifying func, but modifying func [5], because the [] Operator

The priority is higher than *, and func is first combined ). Jump out of the brackets and look at the right side. The parentheses indicate the number of func.

The element of the group is a pointer of the function type. It points to a function that has an int * type parameter, and the return value type is int.

This kind of usage is complicated, and there are many occurrences of this kind of usage, which is often not understandable. I believe the above explanation can be helpful.


Example:

1. Comparison 1:

# Include <iostream>

Using namespace std;

Typedef int (* A) (char, char );

Int ss (char a, char B)
{
Cout <"function 1" <endl;

Cout <a <endl;

Cout <B <endl;

Return 0;
}
 
Int bb (char a, char B)
{

Cout <"function 2" <endl;

Cout <B <endl;

Cout <a <endl;

Return 0;

}

Void main ()
{

A;

A = ss;

A ('A', 'B ');

A = bb;

A ('A', 'B ');
}

2. Comparison 2:

Typedef int (A) (char, char );

Void main ()
{

A *;

A = ss;

A ('A', 'B ');

A = bb;

A ('A', 'B ');
}
 

The results of both programs are the same:

Function 1

A

B

Function 2

B

A

 

* ***** The following is a reference section *****

Reference: http://blog.hc360.com/portal/personShowArticle.do? ArticleId = 57527

Differences between typedef and # define:

Case 1:

Generally, typedef is better than # define, especially when there is a pointer. See the example below:

Typedef char * pStr1;

# Define pStr2 char *;

PStr1 s1, s2;

PStr2 s3, s4;
In the preceding variable definition, s1, s2, and s3 are both defined as char *, while s4 is defined as char, not ours.

The root cause of the expected pointer variable is that # define is a simple string replacement, while typedef is

Type.

Case 2:

In the following code, the compiler reports an error. Do you know which statement is wrong?

Typedef char * pStr;

Char string [4] = "abc ";

Const char * p1 = string;

Const pStr p2 = string;

P1 ++;

P2 ++;

P2 ++ error. Another reminder is that typedef is different from # define, which is not simple.

Text replacement. In the above Code, const pStr p2 is not equal to const char * p2. Const pStr p2 and

Const long x has no difference in nature. It only imposes read-only restrictions on variables, except for the data class of the variable p2.

Type is defined by ourselves rather than the inherent type of the system. Therefore, the meaning of const pStr p2 is: limited number

The p2 variable whose data type is char * is read-only, so p2 ++ is incorrect. Although
However, the author has already explained it clearly here, but I am still confused in this place. I really hope some experts can help me give some advice, in particular, this statement "the Data Type of the variable p2 here is
I have defined rather than the inherent types of the system. "can I modify the types defined by the system instead of the types defined by the system?


Extracted from: http://www.cnblogs.com/csyisong/archive/2009/01/09/1372363.html

 

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.