Is the array name a pointer?

Source: Internet
Author: User

Is the array name a pointer? If not, what kind of structure is the array name? Please explain.

I will describe the differences between array names and pointers that I know.

1. The address is the same and the size is different.
See the following code:
1 int arr [10];
2 int * p = arr;
3 cout <arr <endl;
4 cout <p <endl;
5 cout <sizeof (arr) <endl; // The result is 40
6 cout <sizeof (p) <endl; // The result is 4.
Arr is the array name, and p is the pointer.
The output values of rows 3rd and 4 are the same, that is, both arr and p are the first addresses of arrays. Rows 5th and 6 have different results. The size of arr is the size of the entire array, and the size of p is the size of the pointer.
Why is the size of arr 40?

2. You can use pointers as parameters.
Of course, the pointer parameter is a pointer. An array can be an array or a pointer. The following code demonstrates that the form parameter of an array can be a pointer.
1 void fun (int * p)
2 {
3 cout <p [0] <endl;
4}
5
6
7 int main ()
8 {
9 int arr [10] = {0 };
10 int * p = arr;
11 fun (arr );
12
13 return 0;
14}
This shows that the array name can be used as a pointer.

3. the pointer can be automatically added, but the array name cannot.

1 int arr [10] = {0 };
2 int * p = arr;
3 arr ++;
4 p ++;
When the array name is self-added, the program compilation will fail. From this point, we can see that the array name is a constant (const modifier ).

Www.2cto.com
4. The size of the array name as a parameter is the same as that of the pointer.

1 void fun (int arr [])
2 {
3 cout <sizeof (arr) <endl; // The result is 4.
4 arr ++; // compiled successfully
5}
6
The size of arr is changed to 4, and the arr ++ compilation is successful. The arr as a parameter has completely become a pointer.

The above is the difference between the pointer and the array name I know. If there are other differences, please leave a message to tell me. If anyone knows what structure the array name is, please leave a message to inform me, thank you.

From C Xiaojia
 

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.