Array name and pointer difference (array name is not a pointer, is the first address of the array), array pointer

Source: Internet
Author: User

Array name and pointer difference (array name is not a pointer, is the first address of the array), array pointer

Some time ago, after a C language lesson, the teacher said, "the array name is a constant pointer to the first address of the array ".

I have checked some of them over hundreds of times, including many tutorials and books, all holding the same point of view.

But I always felt that the array name is not equal to the pointer.

 

Practice is the only criterion for testing truth. Here, we have the following content.

 

First, declare an array and a constant pointer and point to that array.

1 int arr [3] = {1, 2, 3}; // declare array 2 const int * p_arr = arr; // declare a constant pointer

 

Q: Is the length of an integer pointer 4 bytes equal to that of an array name?

Printf ("* p_arr length = % d, arr length = % d \ n", sizeof (p_arr), sizeof (arr ));

Running result: p_arr length = 4, arr length = 12

Conclusion: The array name should not be a pointer.

 

Q: The array name cannot be used as the left value. What about constant pointers?

p_arr = p_arr;

Running result: no error is reported.

arr = arr;

Running result: the error -- "=" must be the left value of the operand and the expression must be the left value that can be modified.

Const int * p_arr = & arr [0]; // declare a new one, pointing to the first address of the array
Printf ("p_arr address = % p, arr address = % p \ n", & p_arr, & arr );

Running result: p_arr address = 002DF958, and arr address = 002DF964.

Conclusion: The array name is not a constant pointer, not a constant pointer to the first address of the array.

 

Q: Can the constant pointer be the left value? What about the constant?

1     const int a = 3;2     a = a;

Running result: Error -- "a" cannot assign a value to a constant and the expression must be a modifiable left value.

Conclusion: The constant pointer is not a constant.

 

Q: Is the array name a constant?

Printf ("3 address = % p, arr address = % p \ n", & 3, & arr );

Running result: Error -- the "&" and expression on the constant must be left or a function prompt. (The array name has an address, but the constant does not exist)

# Define B 5 printf ("B address = % p, arr address = % p \ n", & B, & arr );

Running result: Error -- the "&" and expression on the constant must be left or a function prompt. (Same as above)

Printf ("address of a = % p, address of arr = % p \ n", & a, & arr); // a is a constant previously declared with const

Running result: it can run normally, indicating that both a and arr have their own storage space.

1 a = arr [0]; // because arr = arr [0], now the value of arr [0] is assigned to a2 printf ("a address = % p, arr address = % p \ n ", & a, & arr );

Running result: Address of a is 0041F7FC and address of arr is 0041F814 (the two are not in the same memory unit)

Conclusion: The array name is neither a constant nor a constant defined by the const variable.

 

Q: Is the array name equivalent to the first address of the array?

&arr[0] = &arr[0];

Running result: the error -- "=" must be the left value of the operand and the expression must be the left value that can be modified. (The result is consistent with that of arr = arr)

Printf ("arr [0] address = % p, arr address = % p \ n", & arr [0], & arr );

Running result: the address of arr [0] Is 002FFA34, and the address of arr is 002FFA34. (Completely equal addresses)

Printf ("& arr length = % d, arr length = % d \ n", sizeof (& arr [0]), sizeof (arr ));

Running result: & arr length = 4, arr length = 12.

Conclusion: The array name should be the first address of the array, but implicit conversion may be performed when the sizeof operator is used. (Conversion refers to the entire array, that is, sizeof (arr) = the number of elements in the array multiplied by the value of sizeof (arr [0)

 

Conclusion:Currently, the array name is generally the first address of the array. When the sizeof operator is used, the array name refers to the address of all elements in the array.

 

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.