Explore the mysteries of C/C ++ arrays and pointers in depth 3: Anatomy of Arrays

Source: Internet
Author: User

Explore the mysteries of C/C ++ arrays and pointers in depth 3: Anatomy of Arrays

The arrays of C/C ++ are hierarchical, different from those of other languages such as VB. This level is not a dimension, but a one-dimensional array like a famous Russian doll, that is, the nesting of arrays. array elements are also arrays. arrays of VB and other languages are more like a plane.
Array Nesting is a bit strange from the perspective of other languages, but the reason is also very simple. The object model of C/C ++ does not regard an array as a simple set of values, but an object aggregation. Each element is an object. The element is an integer object, an integer array, a floating point object, or a floating point array. However, the array itself is also an object, so an array can also be used as an element of another array. When a one-dimensional array uses a one-dimensional array as an element, each element of this one-dimensional array has an array type. This one-dimensional array is actually a two-dimensional array. Likewise, A one-dimensional array with two-dimensional arrays as elements is actually a three-dimensional array. Therefore, when using the C/C ++ array, we should look at it from the perspective of array nesting. Some people think that the arrays of C/C ++ are not real arrays, and C/C ++ does not have multi-dimensional arrays, compared with arrays in other languages, the two are only different instances of the same thing and are different implementation methods, while the essence is the same, the nested arrays of C/C ++ can be regarded as the development of the array concept.
Now let's take a 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 the element type, D represents the identifier, constant expression must be a constant expression greater than 0, opt represents optional, that is, the content in [] can be blank, if [] is null, it is an incomplete type, indicating that the length of the array object is unknown. The incomplete array type can be supplemented in a certain part of the program. Careful people will immediately find that, in terms of form, how can we only define one-dimensional arrays? How to define multi-dimensional arrays in this form? As I mentioned earlier, arrays in C/C ++ are nested arrays. Therefore, the definition of multi-dimensional arrays also reflects this essence. Multi-dimensional arrays are defined and constructed through nested one-dimensional arrays. For one-dimensional arrays:
T d [m]
When the element is a one-dimensional array T [N], the element type is also an array type. If t [N] is used instead of T, it is:
T [N] d [m]
The syntax structure does not conform to the syntax format defined in the C/C ++ array. After [N] is moved to [m], the formal two-dimensional array is defined:
T d [m] [N]
Where d [0] --- d [M-1] is a one-dimensional array, with the array type T [N]. Multidimensional arrays of various dimensions can be constructed using the same nested method.
A one-dimensional array T [m], after the array to pointer conversion, type conversion to T *, two-dimensional array T [m] [N] After conversion to pointer, the type is converted to T (*) [N]. Some beginners are hard to understand the form T (*) [N]. How can we make such a big difference when we have one dimension, in fact, the principle is related to nesting. Two-dimensional arrays are nested in one-dimensional arrays, and elements are arrays. Therefore, t [N] is used to replace T, the pointer type after the two-dimensional array conversion is t [N] *, and [N] is moved to the right of *, that is, T * [N]. Because [] has a higher priority, therefore, we need to add brackets to T (*) [N]. Otherwise, it is not a pointer type, but a pointer array type.
There are some interesting expressions around the array name. The following describes some important expressions to better understand the array. For two-dimensional arrays:
T a [m] [N]
A: The type of expression A is converted to T (*) [N], representing the first address of the array;
& A: a pointer to a two-dimensional array object. Its type is T (*) [m] [N]. Before the emergence of the C standard, some early implementations do not allow & A, because these compilers believe that A is converted to a right value at this time, and the & operator requires a left value, so it is illegal. The C Standards Committee has extended the concept of objects and allowed & A to be harmless. Therefore, it has written & operator as an exception into the array-to-pointer conversion clause. In this case, a represents the array object, and & A represents the address of the array object. Therefore, the result of & A is the same as that of A, but the type is different.
I have seen some ideas that & A is the first address of the array, not. At the beginning, this seems to be quite reasonable. Isn't the reference of an array object exactly the first address? But in fact, this discussion does not conform to the standard. The conversion from arrays to pointers stipulates that when a points to the initial element of the array object is generated, the premise is that the conversion from array of type to pointer to type, but the type of & A belongs to pointer to array of type, not pointer to type, therefore, the first address of the array is a, not.
(During the previous two days, I accidentally saw a post I replied to in. I thought this question a few times and thought it was far-fetched, now I think this should be better explained: the association between the array name and the first address of the array comes from the implicit conversion clause of the array and pointer, but for & A, Here A is an exception in the clause, there is no such conversion, that is, there is no association between A and the first address of the array. The result of & A is consistent with the first address of a, only because of the nature of & operator, instead of from array name a itself, the meaning of & A is not the first address of the array. --- September January 9, 2011)
& A [0] [0]: Address of the first element of the array. & A [0] [0] is often misunderstood as the first address of array A. In fact, a [0] [0] only has the same address value as a because of its special location, & A [0] [0] is a reference to a T-type object, not an array object reference, and its type is not converted by array of type, therefore, it does not mean the first address of the array.
A [I] (where I> = 0 & I <m): From the Perspective of array nesting, A is a one-dimensional array, and the element type is array type, therefore, the type of a [I] is t [N], and the expression is converted to T *, which is the first address of the I-dimensional array.
A + 1: A is implicitly converted to the pointer type T (*) [N] And then added 1. Remember that the pointer addition is based on the size of the pointer pointing to the object as the step, therefore, a + 1 spans N * sizeof (t) bytes.
& A + 1: similar to a + 1, The & A type is T (*) [m] [N], therefore, the step size of & A + 1 is M * n * sizeof (t ).
Original article: http://blog.csdn.net/supermegaboy/archive/2009/11/23/4855010.aspx.

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.