Original post link: http://www.keepsimply.org/2012/07/06/in-c-arrays-why-is-this-true-a5-5a/
Author: drinking and drinking
Time: 2012.07.05
Disclaimer: the translation is only intended for technical study and communication purposes. If you need to reprint it, please mark the original post link.
Source: http://stackoverflow.com/questions/381542/in-c-arrays-why-is-this-true-a5-5a
The level is limited. If your translation is inappropriate, you are welcome to discuss, criticize, and correct your suggestions.
Post content:
Dinah:
Joel pointed out in stack overflow podcast #34 that in the C programming language (aka: K & R), all mentioned a property of the C array: A [5] = 5 [A].
Joel explained that it was a pointer operation, but I still don't understand why a [5] = 5 [a]?
Mehrdad Afshari: because a [5] is equivalent to * (a + 5), 5 [a] is equivalent to * (5 + ). In elementary school mathematics, we can determine that they are equal.
This is a direct representation of arrays as pointers (this is the direct artifact of arrays behaving as pointers ). "A" is a memory address. "A [5]" is the value of five length elements from. The element address is "A + 5 ". This is the same as the offset from the location of the Five Elements starting from the memory address. The address space is (5 + ).
John Macintyre: I want to know if it is the same as * (5 * sizeof (A) + a). If so, it will be a good explanation.
Mehrdad afshar: Yes. The reason I didn't mention sizeof is that it is easy to explain the problem clearly, and it will become complicated. (I was going to mention the size. But thought I wocould complicate just things to get the core idea .)
Johannes Schaub-LITB: John, no sizeof, the system automatically increments according to the size of sizeof at the offset.
Mehrdad Afshari: @ Dinah, from the perspective of the C compiler, you are right. Sizeof is not required. The expressions I mentioned are similar (sizeof is not required ). However, the compiler will consider sizeof when generating machine code. If a is an int array, the [5] compiler will use an assembly like mov eax and [EBX + 20 ].CodeInstead of [EBX + 5].
David Thornley: Since the array access method is defined as a pointer (access method), a [I] is implemented as * (a + I) and they are interchangeable.
author:
1. C ++ struggle Park once had a similar post, concerned about this problem of friends can go to see, very good. Access to element a [I] [J] in a two-dimensional array
2. I have not completely translated the entire post, and I have not translated it if I have a low score.
3. After translation, I think this post is not well selected and the content is not full enough. I think about whether to translate some articles FOR THE NEXT translation.