C language array element subscript starting from 0

Source: Internet
Author: User

Many classmates may have this question when learning the array, why does the subscript not start from 1? Isn't it more in line with People's daily habits starting from 1? In life we usually say a 1th, not a No. 0. Indeed, some computer languages, such as the early Pascal language, are the subscript for array elements starting from 1. is C language deliberately to be different? To figure this out, first look at how the bottom of the computer handles array elements. We first wrote a small program and then disassembled it in Visual Studio. The source and disassembly sections of the code are as follows:

SOURCE program:

int arr[5]; A global array

int Main ()

{

int i;

for (I =0;i < 5;i++)

Arr[i] = 9;

return 0;

}

Post-disassembly part of the code:

int i;

for (i = 0; i < 5; i++)

0101168E

C7 F8 00 00 00 00

mov dword ptr [ebp-8], 0

01011695

EB 09

JMP 010116a0

01011697

8B F8

mov eax, DWORD ptr [Ebp-8]

0101169A

C0 01

Add eax,1

0101169D

F8

mov DWORD ptr [Ebp-8],eax

010116a0

7D F8 05

CMP DWORD ptr [ebp-8],5

010116a4

7D 10

Jge 010116b6

Arr[i] = 9;

010116a6

8B F8

mov Eax,dword ptr [ebp-8]

010116a9

C7 04 85 80 95 01 01 09 00 00 00

mov dword ptr [eax*4+01019580h], 9

010116b4

EB E1

JMP 01011697

return 0;

010116b6

C0

XOR Eax,eax

In the above table in addition to the program source code, the other part, the leftmost column is the address of the instruction, the middle is the machine code, that is, the last code executed by the machine, the right column is the corresponding assembly language code. It is not possible for beginners to understand these assembly codes. Let's take a look at this sentence:mov dword ptr [eax*4 + 01019580h], 9its function is toArr[i] = 9, whereeaxthe variables are stored in theIthe value,4indicates that each element occupies4bytes,01019580his an arrayarrto confirm that you can type in the Watch Windowarror&arr[0], we can see that their values are equal to0x01019580has shown in the following:




In other words, when assigning a value to the element I, first calculate its address, namely: First address +i*4, in this case, the first address is01019580h, so the first0elements are stored in a01019580hstarting at four bytes, the first1elements are stored in a01019584hin the first four bytes,... .., the first4elements are stored in a01019590hthe start of the four bytes. After calculating the address, use themovdirective will9passed to the address, beginning with the4bytes are stored in a. After executing forafter the loop, view01019580hstart the memory situation as shown in:



As you can see, there are 5 9 bytes in a row starting at 0x01019580h .

as seen from the above, when the array element subscript from the 0 at the beginning, the address of each element is calculated as follows:

Section 0 element Address: First address ( First address + 0*4)

Section 1 element Address: First address + 1*4

Section 2 element Address: First address + 2*4

...

Section I element Address: First address + i*4

when an array element is subscript from the 1 at the beginning, the address of each element is calculated as follows:

Section 1 element Address: First address

                              2 + ( 2-1 ) *4

Section 3 element Address: First address + ( 3-1 ) *

...

Section I element Address: First address + ( i-1 ) *

Obviously, if the array element subscript starts at 1, you will need to do a subtraction operation each time the address is computed. Therefore, in order to improve efficiency, the C language array element subscript starts from 0. C Language of high efficiency is reflected in these points drip, need to learn slowly experience!

C language array element subscript starting from 0

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.