Ranter
Join devRant
Do all the things like
++ or -- rants, post your own rants, comment on others' rants and build your customized dev avatar
Sign Up
Pipeless API
From the creators of devRant, Pipeless lets you power real-time personalized recommendations and activity feeds using a simple API
Learn More
Comments
-
Because indices are not counting, they are offsets. Offset 0 is right at the beginning.
-
When I first started school and saw this in my first class, I knew I was in for the pain that was to come of the next 4 years lmao.
-
i can't remember having a 0 crisis 😄 but maybe this helps:
when you have e.g. an array in your stack and the address of this array is 0x5, then the addresses of your array elements (given they occupy 1 byte each) will be
0x5 + 0 for the first element
0x5 + 1 for the second element
0x5 + n - 1 for the nth element
etc :> -
My crisis was that I didn't understand why you only increment by one to get to the next element if the elements are multiple bytes each. It felt like madness that adding one to a pointer actually adds the size of the type automatically. It didn't take me too long to appreciate the madness.
-
@electrineer I found that pretty obvious, because the compiler does it for me.
What I really fought with as beginner was in assembly, what happens if you store a dword to an address and read back a byte, and why? That went totally over my head. ^^ -
@electrineer Inspect the pointers if you can. The compiler/interpreter is accounting for type size. I played with this in C++.
int* ptr;
qDebug() << ptr << (ptr+1);
This will print out the address and you can see the difference is the pointers is the size in bytes of int. Also note that void* is 1 byte difference because there is no type. -
Explain her in C/C++ terms and it will start making sense.
An array reference is a reference that points to the first item of the array. And when you're iterating over it, using indices you tell it how far from the beginning you want to go. 0=stay at the beginning [skip 0 items]. 1--skip 1 item, and so on -
-
@Fast-Nop that was the thing, it felt like fuckery that the compiler does it on my behalf. But that's very convenient fuckery once you're familiar with it.
@Demolishun this whole thread is about the crises when we were just starting out, lol -
I just accepted it as funny. I used to count 0 and nothing before (for example for file names: photo.png, photo 0.png, photo 1.png... so in this case, photo 1 was the third 💀).
HOWEVER, having to -1 every now and then quickly became old. Like, at the first time. -
Turns out it was me that confused her. I wrote the condition in the for loop as (i = 0; i <= array.length -1; ++)
The -1 she thought was minus-ing the total length of the array -
@bashleigh That's an unusual way of writing that anyway. Usually, I use < instead of <= and drop the -1.
-
@Demolishun I'm not familiar with this. Care to elaborate or share a ref to more info? Google simply gives results how to reverse an array
-
@netikras
In C++:
int array[10];
To point to the 5th int I would do:
array[4]
This is the same as *(array+4). Pointer math.
In C++ an array index is just doing pointer math. If I rearrange the inputs *(4+array) it will point to the 5th int still.
So array[4] is equivalent to 4[array]. This is specific to C/C++. Not sure if other languages allow this kind of shenanigans. -
Hazarth95022yI don't remember a crisis. I just accepted it as natural.
Now when I started working with a language with 1-indexed Arrays (can't remember which) now that was a crisis.... -
She may be interested in Dijkstra’s “Why numbering should start at zero”:
https://cs.utexas.edu/users/EWD/... -
hjk10157312yNot a crisis. Always made sense to me exactly like @Fast-Nop said.
Perhaps that is more obvious in a language like C.
Did make a few off by one mistakes with pointer arithmetic at first. -
@hjk101 In turn, how it's done in C makes sense once you understand assembly because C is the best portable macro assembler ever. ^^
-
because "count" is "how many items is there", and as said, index is "how many places from the start ia the item". first item is zero places from thr start of the array, so it's index zero.
(you do know, of course, that's just my suggestion as to how to explain it to her so that she gets used to it.
also try showing her some algos that iterate through arrays, from start to count, that usually helps)
afaik, i never had a conundrum around this, because from the start i knew about the "index is offset from the start" idea, so it was natural.
oh, maybe try a race analogy: is the start position part of the race track? yes. how far along the race is the start position? 0 meters along the race. the first position that's part of the race is always 0 meters from the start of the race ;) -
oh also show her some distance math. scalar distance (absolute values, distances between two numbers), vector distance... the logic of why the starting point / "the (identity of the) place i'm (pointing) at" always comes out to be zero is a bit more intuitive and natural there for many people.
"when you're standing on the ground, how far above the ground are your feet? when you're standing at the start of an array, how far from the start of an array are you?"
maybe also try the actual technical explanation, array as pointer to a memory location, index as offset added to that location... it's not too difficult even when explained purely technically. -
@electrineer Ive seen the pain that matlab causes. One of our projects at work was to convert R scripts to Matlab. Things were working weirdly and didn't make any sense, until someone discovered that Matlab starts at 1. Then it all made sense.
-
hjk10157312y@Fast-Nop of course I looked at C like a compiler that converts things to ASM but not quite put like that. I like it!!!
Learning assembly really helped me a lot, just knowing about memory segments, registered and how to address them makes a big difference add calling conventions to the mix and I think you know your way around enough that prevent a lot of mistakes. -
hjk10157312y@Midnight-shcode I think that starting out with math will make most beginners quit. The learning curve is steap and area vast enough without adding a CS degree in math to the mix.
-
JsonBoa29832yMine was easy, the teacher even said 'It's like the Christian calendar - if the "third Millenium" starts at 2000*, then the "first Millennium" starts at 0*'
*the millennia do not actually start at 0, but mentioning it would make the saying much harder to remember -
Feibrix932yCount doesn't start at 1. if you have 3 apples and you give 3 apples to me, how many apples do you have? :|
-
Just an example:
When you count seconds, you should start from 0.
If you start from 1 you just counted one second right at the start even though no time has passed.
I was surprised to learn how many people do this wrong. -
@Feibrix if having three apples and giving three apples away occur at the same instant, you still have three apples at that instant by definition.
-
Feibrix932y@Lensflare you mean you only count seconds in your life? Countdowns do not count? If you go to your cellar to find how many bottles of wine you have left and you find 0, how many bottles did you count? Implicit doesn't mean not existent.
-
Feibrix932y@electrineer it doesn't make sense what you just said, you counted thr total amount of apples in the system and not the amount of apples left in your array.
-
I still need longer than I want to admit to get end indices (especially with strings). Fuck that inclusive/exclusive shit.
And yeah, my first few programs printed "ello World" -
@Feibrix obviously it was just an example to show that there are cases where counting from 0 makes more sense than counting from 1.
And as already stated by many others, to better understand array indexes, one should not think of them as counting at all.
Everything is a matter if context and perspective.
Teaching my girlfriend how to code and she’s got to the indexes start at 0 crisis.
Just to make her feel better, anyone else remember their indexes start at 0 crisis? 😅
So far the convo is “why does count start at 1 and index start at 0?!? Developers can’t fucking count”
question