Want to get first element in an array

>want to get first element in an array
>array[0]

how the fuck can coders defend 0 being the FIRST number instead of 1? this is why nobody takes coders seriously.

Attached: facebook_zuck.jpg (600x600, 38K)

Other urls found in this thread:

stackoverflow.com/a/7320709
cs.utexas.edu/users/EWD/transcriptions/EWD08xx/EWD831.html
twitter.com/SFWRedditImages

Well first off "coder" is a shitty term. Most of programming isn't writing code, but thinking. This reeks of meme magical thinking.

Secondly, it has to do with memory and pointers. It's a reminiscence of the past. When you first had an array in C, int *A, let's say, you get the first item by inputting *A. The second by inputting *A + 1, or *A[1]. The first would then be *A[0].

nigger fuck

use matlab you mathematician cuck

You also don't want to be "a coder". To do so is to lock yourself into pajeethood. You write code for reason. Maybe you work in fin-tech, or in game development, or with mathematical research. Heck, maybe even with the psychology of problem solving.

Most "professional coders" are code monkeys, and are equivalent to construction builders in comparison to engineers. People who work solely with software development like with WordPress, Drupal etc.

There are exceptions. But if you're doing web Dev, it's usually code monkeyry.

You get so used to it that after a while it becomes the new normal. Then you feel like it could never be anything different.

LEARN
TO
CODE

#define getidx(arr, i) arr[i - 1]

Attached: 1527553979524.jpg (574x881, 54K)

none of you have explained why 0 is the first number instead of 1 which is the actual first number when it comes to numbers

It's the distance from the first item.

but why is 0 the FIRST item
do coders actually know the number line

1 is only the first natural number baka

stackoverflow.com/a/7320709

Make your own collection if it bothers you.

0 index is pretty handy with for loops though since lenght is always one higher than last index.

*(array + index)

Thinking about the index as "offset-from-zero" simplifies pointer arithmetic. There's basically no other reason other than convention.

it's an offset from a pointer you nigger

To find the address of an array object the compiler do (address of first element + size of type * i) so arr(0) == address of first element

>uses number line as example
Then the first number is negative infinity.

It's the distance from the first item.

The first item is actually 38. But since you index by the distance from the first item, it's 0, 1, 2, 3 etc. I hope that answers your stupid question.

>how the fuck can coders defend 0 being the FIRST number instead of 1?
It's the offset.
array[] is the starting address.
array[0] means you want the data at the address array+0 (at the starting address).
array[1] means you want the data at starting address + 1 (a bit further to the right)

>array.length
>array[1]
>>null pointer exception

Programmers will defend this.

Attached: 1547499890766.jpg (419x396, 39K)

It's just handy. And it's the first unsigned number that you can repsresent on any amount of bits.

That would imply that array is null.

I know! we'll just add 1 to everything so 00 is 1 and FF is 256 every time we need to use numbers, or wrap around so 00 is 256!

Can somebody tell me, at the low level the handful of meme-languages that are 1-indexed, that's just syntax, right? The compiler just pretends the person wrote it in a real language and 0-indexes everything behind the scenes, right?

an array has a natural number of elements.
the first element is in position one.
the second element is in position two.
et cetera.
the first person in a group is person one. the first apple to be picked from a tree is apple one. it is not apple zero, because it is exactly one apple.
1,2,3,4...

an "array" may have a real number of elements, such as 0, but that doesn't make it an array or list of elements. the empty set isn't zero. the position of the first element is position one.

ad homs to be disregarded.

It has a length of 1 though, nerd.

Based and frenpilled

Then why are you throwing null pointer exception?

Null pointer exceptions are only thrown when you try to do something with an element that does not exist (Read: array[1].toString())

Depending on what sort of element you are accessing (primitive or an object) and the language you're using, you won't even get a null pointer exception but rather a default value (which is 0 for primitives and null for objects).

Not to mention your language may be 0 based.

Read the fucking documentation for your environment and deal with it.

Not every high level language maps to the low level characteristics of C
I mean in C the get array item is equivalent to pointer arithmetic where offset makes sense
arr[i] == *(arr + i)

But in the average high level scripting language the "array" is equivalent to a function call
fake_arr[i] == get_nth_list_item(fake_arr, i)

In other words "arrays" in scripting languages are not real C-like low level arrays (which is just values in sequential memory addresses) but high-level data structures (like linked lists or vectors) which can expand freely and don't occupy sequential addresses in memory

>Then why are you throwing null pointer exception?
One guess as to what indexed variable is used in a 1 length array.
Protip; it's not 1.

I love Tom. He was my friend.

Then it would be index out of bounds. Learn your exceptions, faggot.

patrician programming languages index from zero.
patrician math fields usually index from zero.
learn to code or learn to mathematics.

You must have an iq of at least 80 to post here

Attached: uglyfuck.jpg (400x456, 31K)

Attached: hqdefault.jpg (480x360, 11K)

i have been making this thread for years and i don't intend to stop any time soon

You're a brainlet if you don't understand.
array[0] is where the array points to, + offset of 0.
array[n] is offset of sizeof(T) * n.

Super simple answer:
Because starting at 1 would be fucked up in so many ways when you allow variables to be indices.

Say you have a variable that can be 256 possible values. Now all of a sudden only 255 of them are valid indices? Can you even imagine how many additional if-statements and special conditions we'd have to include in code just to make sure you never pass a 0 as an index? And on top of that you're also reducing the total amount of indices a variable can represent by 1.
So starting at 1 would be absolute lunacy.

This is why
cs.utexas.edu/users/EWD/transcriptions/EWD08xx/EWD831.html

This is not why. What you're suggesting would be a non-issue if the convention were 1 to begin with.

because it is?

type Array_Type is array (Integer range ) of Integer;
A : Array_Type (-10 .. -2) := (others => 0);

this confuses the virgin

It's the distance from the first item.

so these are equivalent?
int j[10];
j[4] = 3;
*((int *)(((long)&j) + 4 * sizeof(int))) = 3;

???
thats pretty neat

>It's a reminiscence of the past
Not really, prior to C many languages used 1-based indexing or allowed any ordinal range to index an array.

why not just go with: 1

>First number in base 10 is 0
>How can coders defend 0 being first number?
Though one

>1 ≤ i < N+1
>starting with 0, however, gives the nicer range 0 ≤ i < N
Which is bullshit because 99/100 times you see i < range-1 because of this.

Jesus christ I thought I was dumb. Get some help.

(first array)


Clojure doesn't have this problem. Try a modern language, OP.

What I said still stands

Clojure is a lisp. Lisps never had this sort of indexing in their core (of course people implemented it, but at first it circled around car and cdr)

normal languages all do
for (i = 0; i < array.length; ++i) {
// ...
}
so what do you even mean

You're going to loop past the array with that.

Attached: unknown.png (452x316, 6K)

Are you stupid?

array IS the first element, [0] is the offset from this element. array[0] is the same as *array. Also array[0] is the same as *(array+0), that's why 0[array] is valid syntax in C.

< is not equal to =

Ada must die.

In a simple scenario the first element in memory would be ...000. If you have only, say, 4 bits to work with counting from zero gets you 16 unique codes instead of 15 if we start at 0001. Translating 0000 as '1' could get confusing.

If it makes your panties stay dry, call it 'address zero' as a convention referring to the items postal location rather than sequential count.

Attached: 1542183407139.png (500x500, 151K)

It's an offset.

>coders
>no one takes "coders" seriously

Halfway through baby's first programming book? What are natural numbers?

Maybe consult stack overflow like other script kiddies...Also LURK MOAR

Attached: 1548654473598.jpg (345x336, 214K)

Do you know how decimals work?

>What you're suggesting would be a non-issue if the convention were 1 to begin with.
Did you even read the post you responded to? This makes no sense at all.

If the convention was 1 to begin with how does that change the fact that my numeric variables can still be 0? Are you suggesting we change that convention so that a "char" for instance is any value from 1-256 with no 0?
Because if that's what you're suggesting then I think you don't know anything at all about mathematics and the plethora of problems you would cause by doing that.

It's a tragedy how many serious responses this thread has accumulated

>First language I learned was 1-indexed
>Have to put up with smug nerds literally memeing on reddit about how 0-indexed is so natural
>HAHA 1-INDEX LANGUAGES ALWAYS HAVE TO ADD ONE XD
>lastelem = array[array.length - 1]

array[0] = array location in memory + sizeof(element) * 0

Name a better way to do this you cant