Chapter III Anatomy of an array

Source: Internet
Author: User
Tags array definition

C + + array is different from the VB and other languages of the array, is hierarchical, this level is not the dimension, but like the Russian well-known set of baby, one-dimensional set of one-dimensional, that is, arrays of nested, array elements are arrays, VB and other languages of the array compared to a plane more like.

Array nesting this phenomenon is somewhat peculiar from the perspective of other languages, but in fact the reasons are simple. The object model of C/s + + does not treat arrays as a simple set of numeric values, but as objects that are clustered, each of which is an object. The element is an integer object, an array of integers, a floating-point object, or an array of floating-point numbers. However, the array itself is an object, so an array can also act as an element of another array. When a one-dimensional array takes a one-dimensional array as an element, each element of this one-dimensional array has an array type, and this one-dimensional array is actually a two-dimensional array, in the same vein, a one-dimensional array with two-dimensional arrays as elements is actually a three-dimensional array. Therefore, it should be used in the view of array nesting to use the C + + array. Some people think that the array of C + + is not a real array, and some think C + + does not have multidimensional arrays, these views are biased, compared with other languages, the two are just different instances of the same thing, is the implementation of different methods, and the essence is the same, C + + Array nesting can be regarded as the development of the concept of logarithmic groups.

Now let's look at the definition of the array:

6.5.4 . 2 Array declarators

Semantics

If, in the declaration "T Dl." Dl has the form

D [constant expressionopt ]

This definition is very simple, where t represents an element type, D represents an identifier, constant expression must be a constant expression greater than 0,opt denotes optional, that is, the contents of [] can be empty, and when [] is empty, it is called an incomplete type. Indicates that the length of the array object is unknown, and that an incomplete array type can be added to the entire program somewhere. The attentive person will soon find out, from the formal view, how can only one-dimensional array definition? How does this form define multidimensional arrays? As I said earlier, the array of C + + is nested in arrays, so the definition of multidimensional arrays also reflects this essence. A multidimensional array is defined by a nested one-dimensional array definition. For one-dimensional arrays:

T D[m]

When an element is a one-dimensional array t[n], the type of the element is also an array type, with T[n] instead of T, it is:

T[n] D[m]

This syntax structure does not conform to the syntax of the C + + array definition, and moving [N] to [M] is the formal definition of a two-dimensional array:

T D[m][n]

WHERE d[0]---d[m-1] is a one-dimensional array with an array type of t[n]. multidimensional arrays of various dimensions can be constructed using the same nesting method.

A one-dimensional array t[m], after the array-to-pointer conversion, the type is converted to t*, two-dimensional array t[m][n] converted to a pointer, the type is converted to t (*) [n], some beginners to t (*) [n] This form is more difficult to understand, how many one-dimensional, form there is so much difference, In fact, the principle is still related to nesting, a two-dimensional array is a nested array of one-dimensional arrays, the elements are of array type, so using t[n] instead of T, the two-dimensional array after the conversion of the pointer type is t[n] *, the [N] is moved to the right of the *, that is t*[n], because [] the priority is higher than *, so the need for parentheses *) [N], otherwise it is not a pointer type, but a pointer array type.

There are some interesting expressions around the array name, and the following content deepens the understanding of the arrays by discussing some of the more important ones in these expressions. For two-dimensional arrays:

T A[m][n]

A: The type of a in the expression is converted to t (*) [N], which represents the address of the first element of the array;

&a: is a pointer to a two-dimensional array object of type T (*) [m][n]. Prior to the advent of the C standard, some early implementations did not allow &a because these compilers considered a conversion to an rvalue at this point, while the & operator required an lvalue and was therefore illegal. C Standard Committee Given that the concept of objects has been extended and that &a is not harmful, the & operator is written as an exception in the array-to-pointer conversion clause. In this case, a represents the array object, &a represents the address of the logarithmic group object, so the result of &a is the same as a, but the type is different.

I have seen some views that &a is the first address of the array, not a. This point of view seems to be very reasonable at first, a reference to an array object, is not the first address? In practice, however, this argument is non-conforming, and the array-to-pointer conversion clause stipulates that when generating a points to the initial element of the array object, the precondition is that the array of type to pointer to type The conversion, but the &a type belongs to the pointer to array of type, not the pointer to type, so really represents the first address of the array is a itself, not the &a.

(There's been some debate on this issue these days, okay, let the argument end here.) In line with the principle of respecting standards and removing preconceived prejudices, a is the address of the first element of the array, not the first address of the array---March 27, 2011)

&A[0][0]: This is the address of the first element of the array. &a[0][0] is often misunderstood as the first address of the array A, in fact a[0][0] only because the location is special, its address value is the same as a, &a[0][0] is a T-type object reference, not an array object reference, and its type is not by the array of Type is converted, so its meaning is not the first address of the array.

A[i] (where I >= 0 && i < M): From the point of view of array nesting, A is a one-dimensional array, the type of the element is the group type, so A[i] is of type t[n], and in the expression is converted to t*, which is the first address of the I-dimensional array.

A + 1:a implicitly converts to pointer type T (*) [N] and then add 1, remembering that pointer addition is a pointer to the size of the object, so a + 1 will span N * sizeof (T) bytes.

&a + 1: In the same vein as A + 1, the &a type is T (*) [m][n], so the step of &a + 1 is M * N * sizeof (t).

Chapter III Anatomy of an array

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.