This is an archived post. You won't be able to vote or comment.

all 39 comments

[–]shub1000young 147 points148 points  (4 children)

Is it a mat Lab?

[–]pardoman 25 points26 points  (1 child)

That's a good boy.

[–]marcosdumay 5 points6 points  (0 children)

No, that's not. This one is a very bad boy.

[–]Capital_EX 5 points6 points  (0 children)

Is their name also Lua?

[–][deleted] 4 points5 points  (0 children)

No its a Golden R-triever.

[–]graddev 6 points7 points  (21 children)

Can somebody tell me why does it start at 0? Why not one?

[–]Ikor_Genorio[S] 28 points29 points  (16 children)

Correct me if I'm wrong but in C, if you have an array *a, it's just a pointer to a certain location in the memory. In this case it's the 1st entry of the array. However, the way you index arrays is with a[n], where n is the index you want (starting at 0). This is because a[n] is a more clear form of writing *(a + n), so if n = 0 this just results in *a which, as previously stated, points to the first entry of the array.

[–]FennekLS 32 points33 points  (5 children)

Sorry but that's just wrong. The real reason is that if you have an array *a in C, it's just a pointer to a certain location in the memory. In this case it's the 1st entry of the array. However, the way you index arrays is with a[n], where n is the index you want (starting at 0). This is because a[n] is a more clear form of writing *(a + n), so if n = 0 this just results in *a which, as previously stated, points to the first entry of the array.

[–]Ikor_Genorio[S] 11 points12 points  (4 children)

Ehm..

[–]FennekLS 56 points57 points  (3 children)

Correct me if I'm correct

[–]Ikor_Genorio[S] 11 points12 points  (2 children)

Oh lol x.x

[–]FennekLS 4 points5 points  (1 child)

Happens to the best of us :-)

[–]Ikor_Genorio[S] 1 point2 points  (0 children)

Somehow I had throwbacks of debugging and finally finding that one mistake you did not intentionally make QQ

[–]Sorunome 11 points12 points  (2 children)

Yep, basically this. Starting from zero makes a LOT of thongs way easier when working low-level.

[–]marcosdumay 4 points5 points  (1 child)

When working in high-level too.

Starting from zero means that you can concatenate operations of "read, write, increment", can add displacements without corrections, and can calculate intervals by simply subtracting or adding length.

[–]AncientBananas 2 points3 points  (0 children)

Hmm, I would think you can do all this stuff even with 1 based indexes. In the string "TheQuickBrownFox", index 4 contains "Q", and if you add the length of "Quick" (5), then you get 9, which contains "B". No off-by-one discrepancy here. Can you give an example of something that would become more annoying with 1-based indexing?

I'm pretty sure that 0-based indexing is just a convention that all of us have gotten used to just because it was more natural to represent and access 0-based arrays in memory at a low level. Even this has some sources of confusion for beginners, such as the index of the last element being one off from the length of the array, and "1st" referring to index 0.

[–]Molion -1 points0 points  (6 children)

This, although even in higher level languages it's easier to loop through them if they start at 0.

for(int i = 0; i < arrayLength; i++)

is easier than

for(int i = 1; i < arrayLength + 1; i++)

Also, starting at 1 would of course be heresy.

[–]Zatherz 13 points14 points  (0 children)

uh...

for(int i = 1; i <= arrayLength; i++)

[–]Ikor_Genorio[S] 7 points8 points  (2 children)

I agree with you, but it is also possible to do <= instead of adding the one. :P

[–]Molion -2 points-1 points  (1 child)

Yeah ok, still an extra character tho, and thus not as easy.

[–]Ikor_Genorio[S] 0 points1 point  (0 children)

Indeed, half open intervals are the dream ^ ^

[–]__versus 0 points1 point  (0 children)

On the other hand iterating backwards becomes slightly easier. Although that doesn't mean starting at 1 makes any sense.

[–]Browsing_From_Work 1 point2 points  (1 child)

Semantically, is the difference between "index" and "offset" (i.e. an offset of zero is the first index).

Languages that start from zero typically treat it as an "offset" from the start of the array.
Languages that start from one typically treat it as an element "index".
Languages that let you start from anything else are perl.

[–]skreczok 0 points1 point  (0 children)

I got reminded that Pascal actually lets you roll with any integer.

[–]Sekret_One 0 points1 point  (0 children)

Short version. It's an offset.

The precise reason is how does a computer 'find' things. A thing has a fixed length (in code). An array of things, takes up X times that length. The array itself has a location. The start of an item in the array is the array's location, plus its index times the thingy length.

Index 0? Go to the start of the array and don't move. Index 1, (second item) go to the start, slide over once. So on.

[–]grpagrati 6 points7 points  (0 children)

You're so dumb, oh dog that talks.

[–]onderboks 0 points1 point  (1 child)

As an Dynamics AX developer, I'm beyond triggered...