C pointer (4) parse the c Declaration statement, and

Source: Internet
Author: User

C pointer (4) parse the c Declaration statement, and

(4) parse the c statement

Before continuing to explore the c pointer, it is necessary to parse the complex declaration syntax in the c language.

You only need to remember two rules: one principle and one rule.

Principle: first look at the identifier.

Rule: The operator priority is a rule.


Example

1. The simplest int array [3];

Conclusion: array is an array, array size is 3, and element type is int.

Parsing process: first look at the identifier: array, there is only one operator [], then the array is an array, the element type is int, it is complete.


2. Difficult

(1) array pointer int (* array) [3];

Conclusion: array is a pointer pointing to an array with a size of 3 and an element type int in the array.

Parsing process: first look at the identifier: array. Because the brackets operator is used, it can only be parsed (* array) First, indicating that array is a pointer. If the parsing is complete, you can discard it. Then only [] is left, indicating that the pointer points to an array, and the rest is not explained.

(2) pointer array int * array [3];

Conclusion: array is an array with a size of 3 and the element type in the array is int *.

Parsing process: first look at the identifier: array. Because [] has a higher priority than *, array first combines with [], indicating that array is an array. Only one X is left, indicating that the pointer is stored in the array. The Pointer Points to an int type, that is, the element type in the array is int *.

It seems that the difference is only one (), meaning completely different.

Memory of array pointers and pointer Arrays:



3. More complicated

Function pointer int (* pfun) (int, int );

Conclusion: pfun is a function pointer, that is, pfun points to a function. This function has two parameters: int and int, And the return value of the function is int.

Parsing process: first look at the identifier: pfun. Because the brackets operator is used, it can only be parsed first (* pfun). This indicates that pfun is a pointer. Now only (int, int) is left. It is further explained that pfun is pointing to a function. This function has two parameters, the type is int, and the final int indicates that the return value type of the function is int.


4. More complex

Int * (* pfun [3]) (int, void (*) (int, int *));

Whether you are dazzled or not. Using the above method, you will parse it. You may wish to write it in the comments. Let's take a look.


About typedef

Typedef is used to get a new name for an existing type: Define an alias.

For example, we often see data structures and algorithm series

Typedef int ElemType;

Therefore, where int can be used, ElemType can also be used, for example, the statement ElemType data; Also expresses the meaning of int data.

Typedef is more important to simplify the type name, such

Typedef struct node

{

Int data;

Struct node * lchild;

Struct node * rchild;

} Node, * PNode;

Here, typedef is used to define multiple aliases. Since then, you can directly use Node to declare a struct type, and use PNode to declare a pointer to the struct type. The struct type consists of members as shown above.

In addition, typedef is also used for complex declarations, such

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

The method for parsing this declaration is to parse it completely according to the c declaration method described above. Here p is the function pointer. The prototype of this function is int (int, char );


Column Directory: C pointer



C language (complex type pointer), explain the following statement

Returns the int pointer to a function that accepts the function pointer.
Accepts char * and returns the pointer to the array.
Array with element as function pointer
Pointer to function
Array with element as function pointer
Error Type


C language pointer

A pointer is a special variable. The value stored in it is interpreted as an address in the memory. To understand a pointer, we need to understand four aspects of the pointer: the pointer type, the pointer type, the pointer value, or the memory zone pointed by the pointer, there is also the memory zone occupied by the pointer itself. Let's explain it separately.
First, declare several pointers for example:
Example 1:

(1) int * ptr;

(2) char * ptr;

(3) int ** ptr;

(4) int (* ptr) [3];

(5) int * (* ptr) [4];

If you do not understand the last few examples, refer to the article I posted earlier
Pointer type
From the syntax perspective, you only need to remove the pointer name in the pointer declaration statement, and the rest is the pointer type. This is the type of the pointer. Let's take a look at the type of each pointer in Example 1:

(1) int * ptr; // the pointer type is int *

(2) char * ptr; // the pointer type is char *

(3) int ** ptr; // the pointer type is int **

(4) int (* ptr) [3]; // the pointer type is int (*) [3]

(5) int * (* ptr) [4]; // the pointer type is int * (*) [4]

How is it? Is it easy to find the pointer type?

Type pointed to by pointer

When you access the memory area pointed to by the pointer, the type pointed to by the pointer determines what the compiler will regard the content in the memory area.

In terms of syntax, you only need to remove the pointer name and the pointer declarative * on the left of the name in the pointer declaration statement, and the rest is the type pointed to by the pointer. For example:

(1) int * ptr; // The Pointer Points to an int type.

(2) char * ptr; // The Pointer Points to a char type.

(3) int ** ptr; // The type pointed to by the pointer is int *

(4) int (* ptr) [3]; // The type pointed to by the pointer is int () [3]

(5) int * (* ptr) [4]; // The type pointed to by the pointer is int * () [4]

In pointer arithmetic operations, the type pointed to by the pointer has a great effect.

The pointer type (the pointer type) and the pointer type are two concepts. When you get familiar with C, you will find that, the concept of "type", which is mixed with pointers, is divided into two concepts: "pointer type" and "pointer type", which are one of the key points of mastering pointers. I have read a lot of books and found that some poorly written books bring together the two concepts of pointers. Therefore, the books seem to have conflicts and become more confused.

The pointer value, or the memory zone or address pointed to by the pointer.

The pointer value is the value stored by the pointer itself. This value will be treated as an address by the compiler rather than a general value. In a 32-bit program, the value of all types of pointers is a 32-bit integer, because the 32-bit program's memory address is all 32-bit long. The memory area pointed to by the pointer starts from the memory address represented by the pointer value, and the length is a memory area of si zeof (type pointed to by the pointer. Later, we will say that the value of a pointer is XX, which means that the pointer points to a memory area with XX as the first address. We will say that a pointer points to a memory area, it is equivalent to saying that the pointer value is the first address of the memory area.

The memory zone pointed to by the pointer and the type pointed to by the pointer are two completely different concepts. In example 1, the pointer points to a type that already exists, but since the pointer has not been initialized, the memory zone it points to does not exist, or it is meaningless.

In the future, every time you encounter a pointer, you should ask: What is the type of this pointer? The pointer type is... the remaining full text>
 

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.