| |

VerySource

 Forgot password?
 Register
Search
View: 1397|Reply: 10

Please explain the C language grammar

[Copy link]

1

Threads

5

Posts

5.00

Credits

Newbie

Rank: 1

Credits
5.00

 China

Post time: 2020-10-9 16:00:02
| Show all posts |Read mode
LONG alSubscript[24][4][2];

LONG (*p1)[4][2] = alSubscript;
LONG ((*p2)[4])[2] = alSubscript;

LONG alArray[5][2] = {{1, 2}, {2, -1}, {0, 3}, {1, -1}, {1, -1}};
LONG (*p3[4])[2] = {0, aPPP, 0, 0};


Can the above definition be found in the C language file?
C/C++ is also too flexible and confused
Reply

Use magic Report

0

Threads

17

Posts

11.00

Credits

Newbie

Rank: 1

Credits
11.00

 China

Post time: 2020-10-9 16:15:01
| Show all posts
LONG alArray[5][2] = {{1, 2}, {2, -1}, {0, 3}, {1, -1}, {1, -1}}; this definition is there, Just a common definition
LONG (*p1)[4][2] = alSubscript; this definition is a pointer to an array
Reply

Use magic Report

0

Threads

17

Posts

11.00

Credits

Newbie

Rank: 1

Credits
11.00

 China

Post time: 2020-10-9 16:30:01
| Show all posts
I haven't seen the others. Although they can be compiled and passed, they are more difficult to read.
It is recommended that the host not use this method, personally feel that there is no need to study such expressions. for reference only
Reply

Use magic Report

0

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-10-9 17:45:01
| Show all posts
LONG alSubscript[24][4][2];

LONG (*p1)[4][2] = alSubscript;
A pointer to an array is defined. Each element of the array is a two-dimensional array (with 4 rows and 2 columns)

LONG ((*p2)[4])[2] = alSubscript;
LONG (*p)[4] defines a pointer to a row, each row has 4 elements, and each element is a one-dimensional array with 2 elements, so it can be regarded as similar to the above definition.

LONG alArray[5][2] = {{1, 2}, {2, -1}, {0, 3}, {1, -1}, {1, -1}};
LONG (*p3[4])[2] = {0, aPPP, 0, 0};
A pointer array with 4 elements is defined, which points to a one-dimensional array with 2 LONG elements.
Reply

Use magic Report

0

Threads

9

Posts

8.00

Credits

Newbie

Rank: 1

Credits
8.00

 China

Post time: 2020-10-9 20:30:02
| Show all posts
LONG alSubscript[24][4][2];

LONG (*p1)[4][2] = alSubscript;
//A pointer to a two-dimensional array
LONG ((*p2)[4])[2] = alSubscript;
//A pointer to a pointer to a one-dimensional array

LONG alArray[5][2] = {{1, 2}, {2, -1}, {0, 3}, {1, -1}, {1, -1}};

LONG (*p3[4])[2] = {0, aPPP, 0, 0};
//An array of pointers to a one-dimensional array
================================================= ===============
LONG is defined in <windows.h>
Reply

Use magic Report

1

Threads

5

Posts

5.00

Credits

Newbie

Rank: 1

Credits
5.00

 China

 Author| Post time: 2020-10-9 21:30:01
| Show all posts
水月痕


   LONG alSubscript[24][4][2];

LONG (*p1)[4][2] = alSubscript;
//A pointer to a two-dimensional array
LONG ((*p2)[4])[2] = alSubscript;
//A pointer to a pointer to a one-dimensional array

LONG alArray[5][2] = {{1, 2}, {2, -1}, {0, 3}, {1, -1}, {1, -1}};

LONG (*p3[4])[2] = {0, aPPP, 0, 0};
//An array of pointers to a one-dimensional array
================================================= ===============
LONG is defined in <windows.h>
  

Something wrong

LONG (*p1)[4][2] = alSubscript;
LONG ((*p2)[4])[2] = alSubscript;
These two are the same
Reply

Use magic Report

0

Threads

3

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

Post time: 2020-10-10 00:00:01
| Show all posts
LONG (*p1)[4][2] = alSubscript;
LONG ((*p2)[4])[2] = alSubscript;
These two are the same
---------------------------------
Well, yes, these two are exactly the same.
In C language, there is actually no so-called "multi-dimensional array", but "array of arrays"

p1 is a pointer to an array containing 4 elements, each element of which is an array containing 2 LONG elements.

The method of parsing this definition is the so-called "right and left" rule, which is actually based on the priority rules of the C language.
Reply

Use magic Report

0

Threads

2

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-10-10 04:15:01
| Show all posts
LONG alSubscript[24][4][2];
//alSubscript is
//an array with 24 elements, each one of them is
//an array with 4 elements, each one of them is
//an array with 2 elements, each one of them is
//a LONG

LONG (*p1)[4][2] = alSubscript;
//p1 is
//a pointer, which point to
//an array with 4 elements, each one of them is
//an array with 2 elements, each one of them is
//a LONG

LONG ((*p2)[4])[2] = alSubscript;
//p1 is
//a pointer, which point to
//an array with 4 elements, each one of them is
//an array with 2 elements, each one of them is
//a LONG

LONG alArray[5][2] = {{1, 2}, {2, -1}, {0, 3}, {1, -1}, {1, -1}};
//alArray is
//an array with 5 elements, each one of them is
//an array with 2 elements, each one of them is
//a LONG

LONG (*p3[4])[2] = {0, aPPP, 0, 0};
//p3 is
//an array with 4 elements, each one of them is
//a pointer, which point to
//an array with 2 elements, each one of them is
//a LONG
Reply

Use magic Report

0

Threads

78

Posts

29.00

Credits

Newbie

Rank: 1

Credits
29.00

 China

Post time: 2020-10-10 04:45:01
| Show all posts
LONG alSubscript[24][4][2];
// LONG type three-dimensional array alSubscript

LONG (*p1)[4][2] = alSubscript;
//Define a pointer,
//Pointer to LONG[4][2], a 4*2 array of LONG type

LONG ((*p2)[4])[2] = alSubscript;
// Same as above

LONG alArray[5][2] = {{1, 2}, {2, -1}, {0, 3}, {1, -1}, {1, -1}};
//Define a two-dimensional array and initialize it

LONG (*p3[4])[2] = {0, aPPP, 0, 0};
//Define a pointer array, the elements of the pointer array are pointers to LONG[2]
//And initialize
Reply

Use magic Report

0

Threads

78

Posts

29.00

Credits

Newbie

Rank: 1

Credits
29.00

 China

Post time: 2020-10-10 06:30:01
| Show all posts
Analyze and digest this information:

Excerpts from others:

All complex pointer declarations in C language are composed of various nested declarations. How to interpret complex pointer declarations? The right-left rule is a well-known and commonly used method. However, the right-left rule is not actually the content of the C standard, it is a method summed up from the declaration requirements of the C standard. The declaration rules of the C standard are used to solve how to create a declaration, while the right-left rule is used to solve how to identify a declaration. The two can be said to be opposite. The original English text of the right-left rule says:

The right-left rule: Start reading the declaration from the innermost parentheses, go right, and then go left. When you encounter parentheses, the direction should be reversed. Once everything in the parentheses has been parsed, jump out of it. Continue till the whole declaration has been parsed.


The English translation of this paragraph is as follows:

Right-Left Rule: First look at the innermost parenthesis, then look to the right, then to the left. Whenever you encounter parentheses, you should switch your reading direction. Once you have parsed everything inside the parentheses, jump out of the parentheses. Repeat this process until the entire statement is resolved.

        The author wants to make a small amendment to this rule. It should start reading from undefined identifiers, not from parentheses. The reason for undefined identifiers is that there may be multiple identifiers in a declaration. , But there will only be one undefined identifier.

        Now discuss the application of the right-left rule through some examples, starting with the simplest and gradually deepening:

int (*func)(int *p);

First find the undefined identifier, which is func. There is a pair of parentheses on the outside and an * sign on the left, which shows that func is a pointer, and then jump out of the parentheses. Look at the right side first. It is also a parenthesis. Explain that (*func) is a function, and func is a pointer to this type of function, which is a function pointer. This type of function has formal parameters of type int*, and the return value type is int.

int (*func)(int *p, int (*f)(int*));

func is enclosed by a pair of parentheses, and there is a * sign on the left, indicating that func is a pointer, out of the parentheses, and there is also a parenthesis on the right, then func is a pointer to a function. Such functions have int * and int (*)(int *) Such a parameter, the return value is of type int. Let's take a look at func's formal parameter int (*f)(int*). Similar to the previous explanation, f is also a function pointer. The function pointed to has a parameter of type int* and the return value is int.

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

On the right side of func is an operator [], indicating that func is an array with 5 elements, and there is a * on the left of func, indicating that the elements of func are pointers. Note that the * is not a modification of func, but a modification of func[5 ], the reason is that the [] operator has a higher priority than *, and func is combined with [] first, so * modifies func[5]. Jump out of this bracket and look at the right side. There is also a pair of parentheses, indicating that the elements of the func array are pointers to function types. The function it points to has formal parameters of type int* and the return value type is int.


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

func is enclosed by a parenthesis, and there is another * on the left, then func is a pointer, jump out of the parentheses, and on the right is an arithmetic symbol [], indicating that func is a pointer to an array. Now look to the left, there is an * on the left, Explain that the elements of this array are pointers, and then jump out of the brackets, and there is another bracket on the right, indicating that the elements of this array are pointers to functions. To sum up, it is: func is a pointer to an array, and the elements of this array are function pointers. These pointers point to functions with int* formal parameters and return values ​​of type int.

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

func is a function pointer. This type of function has int* type parameters. The return value is a pointer to an array. The element of the pointed-to array is an array with 5 int elements.

Note that some complex pointer declarations are illegal, for example:

int func(void) [5];

func is a function that returns an array with 5 int elements. But the C language function return value cannot be an array, this is because if the function return value is allowed to be an array, then the thing that receives the contents of this array must also be an array, but the C language array name is an rvalue, it cannot Receive another array as an lvalue, so the function return value cannot be an array.

int func[5](void);

func is an array with 5 elements. The elements of this array are functions. This is also illegal, because in addition to the elements of the array must be the same type, the memory space occupied by each element must also be the same. Obviously, the function cannot meet this requirement. Even if the type of the function is the same, the space occupied by the function is usually not the same.

As an exercise, here are a few complex pointer declarations for the reader to parse. The answer is in Chapter 10.

int (*(*func)[5][6])[7][8];

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

int (*(*func[7][8][9])(int*))[5];

        In practice, when a complex pointer needs to be declared, if the entire declaration is written in the form shown above, the readability of the program will be greatly damaged. Typedefs should be used to decompose declarations layer by layer to enhance readability. For example, for declarations:

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

It can be broken down like this:

typedef int (*PARA)[5];
typedef PARA (*func)(int *);

This makes it easier to see.
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list