The array name takes the address and the array name as the operand of the sizeof operator

Source: Internet
Author: User

Array name take the address is very fun, here to record, if you have different views, welcome message discussion:


in most cases, the array name is converted by default to a pointer to the first element of the array . This is a point that we all know. For example, below:

int array[3] = {n/a};    cout << *array << Endl; cout << array[0] << Endl;

The values for the top two outputs are the same, and at this point the array is converted by default to a pointer to the first element of the array. The value of the first element of the array is obtained by dereferencing the group name. Therefore the values of the two output statements are the same.




This is interesting when the array name is the operand of the address operator, which is not a pointer to the first element of the array by default. Here's a little bit of proof with an example.


(1) The value of the array name as the operand of the fetch address operator and the array name default conversion exponentially the value of the pointer to the first element.

int array[3] = {1, 2, 3};int *ptr = &array;cout << "array =" << array << endl;cout << "ptr = "<< ptr << Endl;

This time we compile, the compiler will generally error, as shown below. This time we look at the error message to know that the array name to get the address of the result is actually a: int (*) [3], indicating that the pointer is pointing to the entire array object, that is, the pointer to the beginning of the array starting address, with the array length and sizeof (int) A data object with the width of the product (I understand that if you have a different opinion, you can discuss it in the comments below). sizeof (int) is analyzed for example programs, and different data types require us to change the operand of sizeof.


(2) For example 1 kinds of error messages, we replace the PTR declaration method to correct the above example:

int array[3] = {1, 2, 3};int (*PTR) [3] = &array;cout << "array =" << array << endl;cout << " ptr = "<< ptr << endl;cout <<" ptr+1 = "<< (ptr + 1) << Endl;

The results of the operation are as follows:



for (1) Kinds of error messages, we can also make the following corrections:

int array[3] = {1, 2, 3};int (*PTR) [3] = &array;int *PTR2 = (int*) &array;cout << "array =" << array << endl;cout << "ptr =" << ptr << endl;cout << "ptr2 =" << ptr2 << Endl;

The output results are as follows:


The two corrections get the same value for the pointer, but the meaning of the two pointers is different, referring to the code below:

int array[3] = {1, 2, 3};int (*PTR) [3] = &array;int *PTR2 = (int*) &array;cout << "array =" << array << endl;cout << "ptr =" << ptr << endl;cout << "ptr2 =" << ptr2 << Endl;cou T << "ptr+1=" << (ptr + 1) << endl;cout << "ptr2+1=" << (ptr2 + 1) << Endl;

The results of the output are as follows:

As you can see, although the values of the array and PTR are the same, the result of using the pointer plus one is completely different. For the value of a pointer that is not cast, plus one gets the pointer value of the space to move the array size backward by the entire array as the base unit. The pointer to the cast plus a length equivalent to the offset of an shaping pointer.



The pointer value after the address of the pointer is not the same as the normal pointer, at this time, plus a 1 offset is an array of space size.



Another particular array is the sizeof operator.   The operand of the sizeof operator is the array name, resulting in the size of the entire array, that is: int array[10]. sizeof (array)/sizeof (int) = 10;

This article is from "Home and Everything" blog, please make sure to keep this source http://louis1126.blog.51cto.com/2971430/1681199

The array name takes the address and the array name as the operand of the sizeof operator

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.