[Interview] I got to the Huawei C interview questions (I had been wondering about this question for a long time. After reading this question, I understood it)

Source: Internet
Author: User
In the past, when I used external array variables, I often encountered errors. Especially when I declared an array and declared it as a pointer as an external variable, I called it!
Now I understand that although the usage of pointers and arrays is basically the same, their underlying implementation mechanisms are different. When a pointer is used to reference an element in an array, the value of the array name represented by the pointer is obtained first, and then the element in the array is obtained using this value .! When we use external variables, this difference is shown! Haha
Come on!

Declare the global variable in file 1: int array [5];
Declare the global variable extern int * array in file 2;
Q: Will an error be reported during the runtime? If yes, will it be during compilation or link?
A: No error is reported during compilation and linking. An error is returned when array [I] is used to access elements in the array!
Cause:
Int array [5]; // informs the compiler that array is an array name and its value is the first address of the element.
Extern int * array; // informs the compiler that array is a pointer. You must know that the pointer is not equal to the array name!

For specific explanations, see the compilation below:
// Extern int array [5]; A [4] = 123;
Movl $123, array + 16
When it is declared as an array, array is an address, and array [I] is actually array + I


// Extern int * array; A [4] = 123
Movl array, % eax
Addl $16, % eax
Movl $123, (% eax)
When declaring a pointer, You Need To dereference the pointer.
That is, the value stored in the pointer + offset is first unreferenced.

?
It's hard to say in just a few words, but expert programming tells me a little bit about it.
The approximate meaning is to first access the array + offset (the array itself is the address) by using the life array ),
Declare the pointer to the array compiler as a pointer. Take the address saved in the array and use the address saved in the array to add the offset. Array itself is the address. It is normal to report an error when the address is retrieved again. Array [0] in your second file, the array is treated as a pointer.

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.