Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Why should the subscript of C language array elements start from 0?

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

What this article shares to you is about why the subscript of C language array elements should start from 0. The editor thinks it is very practical, so I share it with you. I hope you can get something after reading this article. Let's take a look at it with the editor.

Many students may have this question when learning arrays, why not start with 1? Isn't it more in line with everyone's daily habits from 1? In life, we usually say the first, not the zero. Indeed, in some computer languages, such as the early Pascal language, the subscript of array elements begins with 1. Is the C language intended to be different? To figure this out, you have to look at how the underlying computer handles array elements. We first wrote a Mini Program and then disassembled it in visual studio. Some of the source code and disassembled code are as follows:

Source program:

Int arr [5]; / / A global array int main () {int i; for (I = 0

< 5;i++) arr[i] = 9; return 0;} 反汇编后的部分代码: int i; for (i = 0; i < 5; i++) 0101168E C7 45 F8 00 00 00 00 mov dword ptr [ebp-8], 0 01011695 EB 09 jmp 010116A0 01011697 8B 45 F8 mov eax, dword ptr [ebp-8] 0101169A 83 C0 01 add eax,1 0101169D 89 45 F8 mov dword ptr [ebp-8],eax 010116A0 83 7D F8 05 cmp dword ptr [ebp-8],5 010116A4 7D 10 jge 010116B6 arr[i] = 9; 010116A6 8B 45 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 33 C0 xor eax,eax 上表中除了程序源代码,其它部分中,最左列是指令的地址,中间是机器码,也就是机器最后执行的代码,右列是对应的汇编语言代码。可能初学者看不懂这些汇编代码,没有关系。我们重点看一下这一句:mov dword ptr [eax*4 + 01019580h] , 9 它的功能就是arr[i] = 9,其中eax中存放着变量i的值,4表示每个元素占4个字节,01019580h是数组arr的首地址,为了确认,可以在监视窗口中键入arr或者&arr[0],我们可以看到它们的值等于0x01019580h,如下图所示: 也就是说,在给第i个元素赋值时,先要计算它的地址,即:首地址+i*4,在这个例子中,首地址是01019580h,所以第0个元素存放在以01019580h 开始的四个字节中,第1个元素存放在以01019584h 开始的四个字节中,…,第4个元素存放在以01019590h 开始的四个字节中。计算出地址后,用mov指令将9传递到该地址开始的4个字节中存放。执行完for循环后,查看01019580h开始的内存情况,如下图所示:

As you can see, there are five 9s in a row of 20 bytes starting with 0x01019580h.

As you can see from the above, when the subscript of an array element starts at 0, the address of each element is calculated as follows:

0th element address: first address (first address + 04th)

First element address: first address + 1x4

Second element address: first address + 2o4

...

I element address: first address + iTun4

When the array element subscript starts at 1, the address of each element is calculated as follows:

First element address: first address

Second element address: first address + (2-1) * 4

Third element address: first address + (3-1) * 4

...

I element address: first address + (iMur1) * 4

Obviously, if the array element subscript starts at 1, you need to do one more subtraction each time the address is calculated. Therefore, in order to improve efficiency, the subscript of C language array elements starts from 0. The high efficiency of C language is reflected in these dribs and drabs, which needs to be realized slowly in the study!

The above is why the subscript of C language array elements starts from 0. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report