C syntax trap: Two-dimensional array simulation of c Language

Source: Internet
Author: User

From the previous discussion, it is not difficult to draw a conclusion that * a refers to the reference of the element labeled 0 in array. For example, we can write:

1 * a = 84;
This statement sets the value of the element 0 in array a to 84. in the same way, * (a + 1) references the element marked as 1 in array a, and so on. In summary, * (a + I) that is, the reference of the element labeled as I in the array. This method is so common that it is abbreviated as a [I].

It is precisely this concept that makes it difficult for new C language beginners to understand. In fact, a [I] And I [a] share the same meaning as a + I and I +. Some assembly language programmers may find the latter method familiar, but we do not recommend this method.

Now we can consider two-dimensional arrays. As we have discussed earlier, it is actually an array of elements. Although we can write programs that manipulate one-dimensional arrays based on pointers, this is not difficult in the case of one dimension, but it is almost irreplaceable for the convenience of the Two-dimensional array in the form below. Also, if we only use pointers to manipulate two-dimensional arrays, we will have to deal with the "hidden" part of the C language, and often encounter latent compiler bugs.

Let's look back at the previous statements:

1 int calendar [12] [31];
2 int * p
3 int I;
Then you can test yourself. What is the meaning of calendar [4?

Because calendar is an array with 12 array elements and each array element has 31 integer arrays, calendar [4] is the fifth element of the calendar array, it is one of the 12 arrays with 31 integer elements in the calendar array. Therefore, the action of calendar [4] is an array with 31 integer elements, for example, the result of sizeof (calendar [4]) is the product of 31 and sizeof (int.

1 p = calendar [4];
This statement points the pointer p to the element marked as 0 in the array calendar [4. If calendar [4] is an array, you can specify the elements in the array in the following way:

1 I = calendar [4] [7];
We can do the same. The statement can be written as follows, and the meaning of the expression remains unchanged:

1 I = * (calendar [4] + 7 );
This statement can be further written as follows:

1 I = * (calendar + 4) + 7 );
From this we can easily find that the subscript form in square brackets is much easier to express than the pointer. Let's look at it again:

1 p = calendar;
This statement is invalid because calendar is a two-dimensional array, that is, an array. In this context, using the calendar name will convert it into a pointer to an array, p is a pointer to an integer variable. This statement attempts to assign a pointer of A type to another type, so it is invalid.

Obviously, we need a method to declare a pointer to an array. After a discussion of similar issues, we should not waste much effort to construct the following statement:

1 int (* ap) [31];
The effect of this statement is that * ap is an array ap with integer elements. ap is a pointer to this array, so we can write it like this:

1 int (* monthp) [31];
2 Monthp = calendar;
In this way, monthp points to the first element of the array calendar, that is, one of the 12 array type Elements with 31 elements in the array calendar.

Assume that we need to clear the calendar array at the beginning of the new year, which can be easily done in the subscript form:

1 int month;
2
3 for (month = 0; month <12; month ++ ){
4 int day;
5 for (day = 0; day <31; day ++)
6 calendar [month] [day] = 0;
7}
What should I do if the above Code segment is represented by a pointer? We can easily set the value of "calendar ar [month] [day] = 0;" to * (calendar Ar + month) + day) = 0;

But what are the really relevant parts?

If the pointer monthp points to an array with 31 integer elements, and the calendar element is also an array with 31 integer elements, therefore, just like in other cases, we can use a pointer to traverse an array. Here we can also use the monthp pointer to traverse the array calendar in a step-by-step manner:

1 int (* monthp) [31];
2 for (monthp = calendar; monthp <& calendar [12]; monthp ++ ){
3 int * dayp;
4 for (dayp = * monthp; dayp <& (* monthp) [31]; dayp ++)
5 * dayp = 0;
6}
So far, our journey has almost been like a thin ice, and we have already gone a long way. Before we fall, we 'd better take a long time to complete. Although the last example in this section is a valid ansi c program, the author also finds a compiler that allows the program to pass compilation smoothly: currently, most c compilers can accept the code in the above example ). Although some of the above examples deviate from the topic of this book, this example can reveal the unique relationship between arrays and pointers in C language, so that the two concepts can be elaborated more clearly.

Related Article

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.