Why do arrays start at 0?

Why do arrays start at 0?

Attached: 1551288961894.png (1013x1072, 503K)

Other urls found in this thread:

godbolt.org/z/gKU9DQ
quora.com/Why-do-array-indexes-start-with-0-zero-in-many-programming-languages
twitter.com/SFWRedditGifs

why not

Attached: 1551351954051-vg.jpg (220x346, 15K)

Memory offset.

It's an offset from the first element.

Because computers start counting at zero

Because every other thing in this world also starts at 0

because some brainlets took over programming

this post is made by 1-based array indexing gang

why does my fren count start at 0

Attached: a38.jpg (1024x962, 67K)

This post was made by reddit gang

fren++

Attached: serveimage.png (331x152, 9K)

I guess so that you can store your array as a pointer to a bunch of sequential data in memory and then access elements with an offset with minimal work for the compiler/runtime

fren--

Attached: efd[1].jpg (403x392, 20K)

...

>What's your home address?
Say it's 10 whatever street
>What's the next house address?
It's (your address number)+1 whatever street
The house before yours is (your address number)-1 whatever street
You can think of your house being (your house address)+0

Arrays are like streets starting at your house, we just want to know how many houses down from yours we want to visit
Your address is array[0]
The next is your address + 1, so array[1]

thank fren :)

Attached: jqafiyphicj21.jpg (604x516, 30K)

Yeah except there's no such thing as 0 Whatever Street, dumbass

you can always have the first element as a null or 0 and start every loop to access the array at 1, and the array size be reqsize+1.

also post pics of you in your "programming socks"

Please learn to read

So you can have one more element in your array without sacrificing memory since, you know, binary

Because with arrays starting at 1, you can't get an array of a full size of your index. For example, with a 16 bit unsigned integer index, you can only get 65535 elements, but not 65536 (64K), since there's no element 0. With a 32-bit index, you can only get 2^32-1 elements, but not 2^32. It used to be big when 16- and especially 8-bit integers were a thing due to various hardware constraints, long time ago. So the convention in C (a fairly low level language, even at the time) was to start array indexes at 0, for the maximum index capacity.

Array indexes do start at 1 in some (usually less close to the metal) languages though - Algol and Pascal being an example. Arbitrary/ranged indexes and many other index abstractions are also a thing.

the single best language in which arrays start at 1 is Lua

Mainly C's influence.

Arrays in C (and most other languages) are implemented as a continuous block of memory that contains fixed-size elements in order. An array variable in C is basically a pointer to the start of this block of memory.

What happens when you want to retrieve the 3rd element for example, is that you take the pointer that points to the whole block of memory and add 2 * to it. `array[2]` is basically syntax sugar around `*(array + 2)`, 2 is the offset with respect to the pointer to the start of the array.

bump that iterator once more

Attached: 1538751461924.jpg (439x363, 127K)

learn how to build your own processor and write create its assembler.
You'll understand.

It's how the underlying memory works. If you implemented an array with a 1 index and indexed it using a non-constant variable, you would have to subtract from the pointer at runtime, which is an extra step that needs to be done every time you reach into the array.
In a tight loop, that's unacceptable.

Because C popularized it.
There is no rational reason to keep it that way nowadays. Indexing should start at 1. It makes more sense and it's more logical.
>b-but that's not how computerz werk!!!
It is only if you're using raw arrays, contiguously allocated in memory, and even then, indexing is such a basic abstraction that the implementation should be able to deal with it easily.
There is no logical reason why a generic list-like data structure should start at 0.

modern processors can do the offset with no extra cost as part of the same instruction

Because everything starts at 0.

godbolt.org/z/gKU9DQ

Seems you're right. That's pretty nice.
Anyway, this was the historic reason why it was implemented that way. I guess modern languages could do better.

"No"

I like this thread fren

Attached: 432435341234.jpg (1000x800, 61K)

I just suddenly realized !
Made my day, made me feel smart,
thanks user

Attached: 689e79338eaccea4f44498719fca9abc.jpg (955x960, 49K)

It's a waste of bits otherwise.

What if you want to remap a bunch of values using an array, and one of those values is zero?

well quora.com/Why-do-array-indexes-start-with-0-zero-in-many-programming-languages

this awnser looks fine. one is that the way C alocates memory to create arrays the other one is math.

the memory allocation part isn't relevant on modern computer as was already pointed out, and the "math" reason is opinion, a valid opinion for some cases maybe but not for most of the programming people actually do
people end up having to type length-1 all the time, much less than they would have to deal with empty ranges

Dis.rite.hare.

not in my language

Everything starts at zero user.

One reason is because the index is an offset. An arguably more significant reason is because index arithmetic tends to be cleaner when 0-based.

Because the inventors of programming are fucking retarded.

Older programming languages start arrays at 1. Arrays in Fortran, Algol, PL/I, Pascal and Ada can start at any number. Pascal and Ada can also use enumeration types for array indexing.

[10, 20, 15:30, 0:100]INT a;
FOR n TO 4 DO print((n LWB a, n UPB a, new line)) OD

type Day is (Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday);
subtype Weekday is Day range Monday..Friday;
A : array (Day) of Natural;
B : array (Weekday) of Boolean;
C : array (Monday..Wednesday) of Character;

why do dates start with months instead of days?

>t. american

>array[10]
>ends at 9

defend this

Because make it begin in 1 will consume more memory (you can do it) and 0 isn't a negative signed number