Pointer array and array pointer

xiaoxiao2021-03-06  14

Today, suddenly, the syntax of the array pointer has produced questions, review it, prevents the progress of the speed.

Char a [10]; // (1) A 1-dimensional number of groups containing 10 char [10] char * b [10]; // (2) A 1-dimensional number of group char * containing 10 char * elements [10] Char (* c) [10]; // (3) a pointer char char (*) pointing to 1-dimensional array of 10 char elements [10]

Priority order:

() [] * Char (here the char represents all basic types such as int, long, double, etc.)

Pointer statement: char * p; char (* p); // According to priority, two equivalents. Here, (* p) is used as an overall understanding. The type after his P is taken is char, then his type is of course point to Char's pointer char *.

Analytical method:

For (1) [] first explanation A is an array, then CHAR description of the type of array For (2) [] first explanation B is an array, then char * Describe the type of array for (3) (* c) It is a pointer, followed by converting A in (1) to (* c), the declaration (* c) is a 1-dimensional array C containing 10 char elements to pointing to its pointer.

For (1) Char a [10]; A and & A value are the first address of the array, but the type is different. The type of A is char [10]; & A type is char (*) [10];

Char (* D [10]) (int); // (4)

Due to the highest priority, it is now understood (* d [10]) is a function of returning a CHAR type parameter is a function of an int type, then press (2) to obtain D is a pointer array, where the pointer points to a return CHAR type parameters are a function of an int type.

When defining variables, the type cannot be added parentheses

Char a [10]; char (* c) [10]; c = (char (*) [10]) a; // correct

(char (*) [10]) C; // error (int) foo; // error

转载请注明原文地址:https://www.9cbs.com/read-48946.html

New Post(0)