Why arrays start from 0




















The name of an array is the name of the array; contrary to the common misconception, arrays are not pointers in any sense. An array expression such as the name of an array object is usually, but not always , converted to a pointer to the first element. Example: sizeof arr yields the size of the array object, not the size of a pointer. While you obviously didn't reacted to KeithThompson's comment, I would like to you use a more offense course: " In C, the name of an array is essentially a pointer, a reference to a memory location " - No, it is not.

At least not in a generic point of view. While your answer perfect answers the question in a manner how 0 as index start is important, the first sentence is plain incorrect. An array does not always decay to a pointer to its first element. Quote from the C standard, C18 , 6. If the array object has register storage class, the behavior is undefined.

Also this decay is happen in a more "implicit" or "formally" way than suggested here; there is no decay to a pointer object in memory involved. This is object of this question: Is the array to pointer decay changed to a pointer object? Add a comment. This question was posted over a year ago, but here goes About the above reasons While Dijkstra's article previously referenced in a now-deleted answer makes sense from a mathematical perspective, it isn't as relevant when it comes to programming.

This can be tested quite easily. Conclusion The decision to start count at 0 , has since permeated all digital systems , including the software running on them, because it makes it simpler for the code to translate to what the underlying system can interpret.

Quoting from the paper:. What if they had just removed the bit Are you actually suggesting modification of basic arithmetic to make it fit in? Don't you think what we have today is a far better solution?

Years later my 2 cnt worth. This crops up in math and programming quite often. Maybe it is just me or the field in which I work. Even today you can still write 2[a] instead of a[2]. This would have required 2 additions on CPUs of the time instead of 0, meaning half the speed. Clearly not desirable.

Just because you want 8 states, it does not mean you must have the number 8 in them. The light switches in my house are happy to represent "light on", "light off" states, without ever wondering why they don't represent the number 2.

Show 4 more comments. Because 0 is how far from the pointer to the head of the array to the array's first element. Doug T. Because 0-based index allows Branko Dimitrijevic Branko Dimitrijevic You bring up an interesting point.

It can hurt performance. But will the performance hit be significant to justify use of 0 as starting index? I doubt it. FirstNameLastName 1-based indexes offer no advantage over 0-based indexes yet they perform slightly worse. That justifies 0-based indexes no matter how "small" the gain is. Yes, i understand that small things can add up and sometimes become a big thing.

But, if 2 billion people donate it, then we can do a lot of good for humanity. I am looking for a similar example in coding which could cause poor performance. Rather than subtracting 1, you should use the address of the array-1 as the base address. That what we did in a compiler I once worked on. That eliminates the runtime subtraction. When you're writing a compiler, those extra instructions matter a lot. The compiler will be used to generate thousands of programs, each of which may be used thousands of times, and that extra 1 instruction may occur in several lines inside an n squared loop.

It can add up to billions of wasted cycles. No it wont hurt the performance once it is compiled, it will only add a small build time because in the end it will be translated to machine code.

It will only hurt the compiler designers. Show 1 more comment. Because it made the compiler and linker simpler easier to write. Reference : " Referencing memory by an address and an offset is represented directly in hardware on virtually all computer architectures, so this design detail in C makes compilation easier" and " When you're writing a compiler, those extra instructions matter a lot.

The compiler will be used to generate thousands of programs, each of which may be used thousands of times, and that extra 1 instruction may occur in several lines inside an n squared loop.

It can add up to billions of wasted cycles. However, E. Dijkstra later wrote a pertinent note, why numbering should start at zero in To denote the subsequence of natural numbers 1, 2, Dijkstra argues that the proper notation should be able to denote naturally the two following cases : 1. The subsequence includes the smallest natural number, 0 2. The subsequence is empty.

So we are left with a and c. Construct array with sum of product of same indexed elements in the given array equal to zero. Check if every index i has an index j such that sum of elements in both directions are equal.

Number of permutations such that sum of elements at odd index and even index are equal. Count of contiguous subarrays possible for every index by including the element at that index. Check if it is possible to reach to the index with value K when start index is given.

Article Contributed By :. Harish Kumar Easy Normal Medium Hard Expert. Writing code in comment? Please use ide. Load Comments. What's New. Most popular in Arrays.



0コメント

  • 1000 / 1000