Array and array address

Source: Internet
Author: User

Problem:

Int Ia [2] = {1, 2 };
Cout <& Ia <Endl;
Cout <& Ia [0] <Endl;

The running result is as follows:
0012ff38
0012ff38
I would like to ask, if the value of IA is the address of the first element of the array, that is, & Ia [0], then IA should also have an address as a variable, why is the value of this address the same as that of & Ia [0? If
* (& IA) = * (& Ia [0]) = A [0] = 1; (because the value of & IA is equal to that of & Ia [0)
But is it true that * (& IA) = IA = & Ia [0] = 0012ff38?
At 0012ff38, the integer 1 (* (& Ia [0]) = Ia [0] = 1) is stored ),
Or is the address value 0012ff38 (* (& IA) = IA = 0012ff38?
I have not figured it out?

========================================================== ========================================================== ======

Answer:

Array is a type, which is beyond doubt. For example, in int Ia [2] = {1, 2};, the type of IA is int [2].
Therefore, IA is a variable, which is undoubtedly a variable of the int type [2.
Since it is a variable, We need to allocate memory space and there is an address. This is also true. In this example, the address of the array variable IA is 0012ff38. It is the IP address of the int [2] variable IA and the IP address of the int variable Ia [0], so it is easy to understand & IA = & Ia [0, this is because the memory of the array is allocated.
The problem is why (IA = 0012ff38 ). Here we need to consider the special nature of the array type. Yes, IA is an array variable, but IA can be implicitly converted into a pointer constant (Note: T * const, not const T *, that is, the address saved by the pointer itself will not change, always & IA, but the value on this address, that is, Ia [0] is variable ).
Specifically, if int [2] is a class int_2, the compiler writes int_2 like this:
Class int_2
{
....
Public:
Operator (int * const) () {return (int * const) This ;}
....
}

Therefore, when you access Ia directly, you can see that the value is actually its this pointer value, that is, Ia ==& IA. Its essence is:
Int * const Pia = & IA;
Pia = & IA
Of course it is correct.
This implicit conversion is provided to facilitate array access. For example, Ia [1] is equivalent to * (IA + 1 ).
Int * const Pai = & IA;
* (PIA + 1)
Of course, you can access the element whose subscript is 1.
(Note that a [n] And * (a + n) are always equivalent. For example, you will find that IA [1] = 1 [Ia]. in almost all compilers, A [n] is first translated into * (a + n) before further compilation)
Implicit conversions are automatically completed by the compiler when necessary. For example, when we do * (IA), because int_2 does not provide * operators, and all pointers provide operators, we convert IA to (int * const) & IA. Similarly, * (IA + 1) is converted to complete the + operation. Ia [1] defines
Int & int_2: operator [] (size_t idx) {return * (IA + idx);} implicit conversion is performed within the function body.

Therefore, it is not necessary to find out that IA = & IA is an implicit conversion to complete the = operation.
The author's questions are as follows:
1. & IA = & Ia [0] = 0012ff38 was established because the addresses of IA and IA [0] are both 0012ff38.
2. the integer 1 is stored at 0012ff38, because Ia [0] = * (IA) = 1 // here * (IA) is implicitly converted.
3. * (& IA) = IA = & Ia [0] = 0012ff38 was established,
* (& IA) = IA is the same, and IA = & Ia [0] is implicitly converted on the left.
4. * (& IA) = * (& Ia [0]) = A [0] = 1 is not valid !!
The assumption is based on & IA = & Ia [0]. But although they are an address, they are not pointers of the same type! Different types of pointers can be converted to size_t for comparison during ==evaluation, but the type difference is displayed when they perform & Operation separately. & IA type (INT [2]) *, or as int_2 * as mentioned above *. The & Ia [0] type is int *. In this case, the type of * (& IA) is int_2, and the type of * (& Ia [0]) is int. Int2 itself does not define the = operation. It can only be implicitly converted to int * Const. In this case, its value is completely different from * (& Ia [0.
So * (& IA) = 0012ff38 is correct, * (& IA) = 1 is incorrect. This is not a conflict, as long as you know that the compiler defines an array to the implicit conversion of pointer constants.

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.