Consider this example:
int calendar[12][31];
The statement declares that the calendar is an array that has 12 array elements, each of which is an array of 31 integral elements (not in turn). Therefore, the value of sizeof (calendar) is the product of 12*31=372 and sizeof (int). If the calendar is not an operand for sizeof, but is used for other occasions, then the calendar is always converted to a pointer to the starting element of the calendar array. To understand the meaning of the above sentence, you must first understand some details about pointers.
Any pointer is a variable that points to a type.
when int a[3];
int *p;
If you write this: P = A;
The address of the element labeled 0 in array A is automatically assigned to p. Note that we did not write
p = &a;
This notation is illegal in ansic because &a is a pointer to an array, and P is a pointer to an integer variable whose type does not match.
Now continue to consider the first "two-dimensional array", which is actually an array of elements as an array.
Several statements are as follows:
int calendar[12][31];
int *ip;
int i;
What is the meaning of calendar[4]? Because the calendar is an array of 12 array-type elements, each of its array-type elements is a set of 31 integer elements, so calendar[4] is the fifth element of the calendar array, is one of 12 arrays with 31 integer elements in the calendar array. Thus calendar[4] behaves as a behavior with 31 integral elements. For example, the result of sizeof (Calendar[4]) is 31 and the product of sizeof (int). Another example,
p = calendar[4];
This statement causes the pointer p to point to the element labeled 0 in the array calendar[4].
If CALENDAR[4] is an array, we can of course specify the elements in this array in the form of subscripts, like,
i = calendar[4][7];
We can do that, too. or similar to the previous one, this statement can be written as follows and the meaning of the expression remains the same:
i = * (CALENDAR[4]+7); This statement can also be expressed as:
i = * (* (calendar+4) +7);
Look below,
p = Calendar;
This statement is illegal. Because the calendar is a two-dimensional array, an array of arrays, using the calendar name in the context here will convert it to a pointer to an array, and P is a pointer to an integer variable that attempts to assign a pointer of one type to a pointer of another type. So it's illegal.
*********************
Obviously we need a way to declare pointers to arrays:
int (*AP) [31];
This statement actually declares that *AP is an array with 31 shaping elements, so the AP is a pointer to such an array. And so we write,
int calendar[12][31];
int (*MONTHP) [31];
MONTHP = Calendar;
In this way, MONTHP will point to the first element of the calendar, which is one of the 12 array type elements with 31 elements in the array calendar.
Assuming that at the beginning of the new year, we empty the calendar array, the subscript form can be easily done, int month;
for (month = 0;month < n; month++) {int day;
for (day = 0;day <31;day + +)
Calendar[month][day] = 0;
}
What does the above code say with pointers?
Calendar[month][day] = 0;
expressed as
* (* (calendar+month) +day) = 0;
But what is the part that is really relevant?
If MONTHP points to an array with 31 integers, and the calendar element also has an array of 31 integers, we can use a pointer
To iterate over an array, here we can also use the pointer monthp to iterate through the array in the form of a calendar:
int (*MONTHP) [31];
for (MONTHP = CALENDAR;MONTHP < &calendar[12;monthp++])/* processing for one months */
Similarly, we can handle the elements of the array pointed to by the pointer monthp, just like the other arrays:
int (*MONTHP) [31];
for (monthp = calendar; monthp < &calendar[12]; MONTHP + +) {
int *DAYP;
for (DAYP = *monthp;dayp<& (*MONTHP) [to];d ayp++)
*DAYP = 0;
}
C Language Pointers and arrays