C Language Array Introduction to the array of declarations and two-dimensional array of analog _c language

Source: Internet
Author: User
Tags arrays

The link between the two concepts of pointer and array in language is so inseparable that if one cannot be understood, another concept cannot be fully understood.

The following two points should be noted for the array in C language:

The C language has only one-dimensional arrays, and the size of the array must be determined as a constant at compile time. However, the elements of an array in the C language can be any type of object, and of course it can be another array. In this way, it is not difficult to "simulate" a multidimensional array.
For an array, we can only do two things: Determine the size of the array, and get a pointer to the element labeled 0. Other operations on arrays, even if they appear to be operating as an array subscript, are actually done through pointers. In other words, any array subscript operation is equivalent to a corresponding pointer operation, so we can completely define the behavior of the array subscript according to the pointer behavior.
Once we have thoroughly understood these two points and their implied meaning, the understanding of the C-language array operation is simply "a piece of cake". If the above two-point content is not clear, then the C-language array operation may cause many confusion to the programmer. In particular, programmers should have the ability to integrate array operations with their corresponding pointer operations, which can be easily switched in the brain when thinking about problems. No obstruction.

Indexed operations are built into any programming language, and index operations are defined in the form of pointer arithmetic in the C language.

How to declare an array

To understand the operating mechanism of arrays in C, we first have to understand how to declare an array, for example:

int a[3];

This statement declares that a is an array of 3 integer elements, similar to the

struct{
 int p[4];
 Double x;
} B[17];

Declares that B is an array of 17 elements, each of which is a struct that includes an array with 4 cosmetic elements (named P) and a variable with a double (named X).

Now consider the following example:

int calendar[12][31];

This statement declares that the calendar is an array that has 12 elements of an array type, each of which is an array of 31 integer elements (instead of an array of elements with 31 array types). Each element is an array of 12 integer array elements, so the value of sizeof (calendar) is the product of 372 (31*12) and sizeof (int).

If the calendar is not for sizeof operands, but for other occasions, the calendar is always replaced with a pointer to the starting element of the calendar array. To understand the meaning of the above remark, we must first understand some details about the pointer.

Two-dimensional array simulation
*a is the reference to the element labeled 0 in array A. For example, we can write this:

*a=84;

This statement sets the value of the element labeled 0 in array A to 84. Similarly, * (a+1) The reference to an element that is labeled 1 in the array A, and so on, and, in general, * (A+i) is the reference to the element in the array labeled I, which is so commonly used and therefore denoted as a[i].

It is this concept that makes C-language novice difficult to understand, in fact, because A+i and i+a the same meaning, so a[i] and I[a] also have the same meaning. Perhaps some assembly language programmers will find the latter kind of writing familiar, but we definitely do not recommend this type of wording.

Now we can consider two-dimensional arrays, as discussed earlier, it is actually an array of elements, although we can also write a program that manipulates a one-dimensional array entirely according to the pointer, which is not difficult in one-dimensional situations, but is almost irreplaceable for the convenience of two-dimensional arrays from notation. Also, if we only use pointers to manipulate two-dimensional arrays, we will have to deal with the most "obscure" parts of C and often encounter the lurking compiler bugs.

Let's go back and look at a few more statements:

int calendar[12][31];
int *p
int i;

Then a test of their own, calendar[4] what is the meaning of it?

Because calendar is an array of 12 array type elements, each of which has a 31 integer array, so calendar[4] is the fifth element of the calendar array, is one of 12 array of 31 integer elements in the calendar array, so the behavior of calendar[4] behaves as an array of 31 shaping elements, such as sizeof (Calendar[4]), and the result is the product of 31 and sizeof (int).

P=CALENDAR[4];

This statement causes the pointer p to point to the element labeled 0 in the calendar[4 of the array. If CALENDAR[4] is an array, we can, of course, specify the elements of the array in the form of subscript, as follows:

i = calendar[4][7];

We can indeed do so. or similar to the previous one, this statement can be written as follows and the meaning of the expression remains unchanged:

i = * (calendar[4]+7);

This statement can also be further written:

i = * (* (calendar+4) +7);

It is not difficult to find from here that the subscript form of square brackets is obviously much simpler than the pointer to express. Let's look at the following:

p = Calendar;

This statement is illegal, because calendar is a two-dimensional array, an array of arrays, using the calendar name in this context translates it into a pointer to an array, and P is a pointer to an integer variable that attempts to assign a pointer of one type to another type of pointer , so it is illegal.

Obviously, we need a way to declare pointers to arrays, and after a lot of discussion about similar problems, the following statements should not be wasted:

int (*AP) [31];

The effect of this statement is to declare that *AP is an array of 31 integer elements, and that the AP is a pointer to such an array, so we can write:

int (*MONTHP) [to];
MONTHP = Calendar;

In this way, MONTHP will point to the first element of the array calendar, which is one of 12 array-type elements with 31 elements in the array calendar.

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

Int month for

(Month=0;month < 12;month++) {
 int day;
  For (Day=0 Day < 31;day++)
   calendar[month][day]=0;
}

What does the code snippet above say if you use a pointer? We can easily put calendar[month][day]=0; expressed as * (* (calendar+month) +day) = 0;

But what are the real parts?

If the pointer monthp to an array of 31 integer elements, and the calendar element is an array of 31 integer elements, it is like in other cases we can use a pointer to traverse an array, Here we can also use the pointer monthp to walk through the array calendar in a step-by-step fashion:

int (*MONTHP) [to];
For (MONTHP=CALENDAR;MONTHP < &calendar[12];monthp++) {
 int *dayp;
 For (Dayp=*monthp;dayp < & (*MONTHP) [to];d ayp++)
 *dayp=0;
}

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.