Why Do Arrays Start at 0? #programming #lowcode #tech #codinglessons #security

preview_player
Показать описание

Pointer Math 📚🚀 #Cplusplus #CodingTips #OperatorOverloading #MatrixMultiplication #CodeTricks

🔥🔥🔥 SOCIALS 🔥🔥🔥
Рекомендации по теме
Комментарии
Автор

Also Dijkstra (yes, that Dijkstra) has a paper on this called “Why numbering should start at zero”

dereotu
Автор

Put the video of your face on the bottom, most of the code is hidden behind the title and junk in the mobile app.

daviddemmers
Автор

More generally, a[x] is the same as *(a + x), hence why you can also use x[a]. Under the hood, the compiler knows that when adding an integer to a pointer, the integer should first be multiplied by the size of the object the pointer points to.

NuclearCraftMod
Автор

This was mind blown, now I can confuse friends with C++ too, of course it works because it works in C.

Yupppi
Автор

A good example i was taught in college is: when you turn x age you have had x+1 birthdays because of the day you were born (0)

brosauce
Автор

Incorrect, the address 2 is not being offset by test.

Rather, the compiler knows which component of the constructions array[index] and index[array] is an address in memory and which is an integer.

The compiler performs pointer arithmetic in both cases, wherein the address in memory is offset by the index multiplied by a scalar equal to the size of the array's type.

array[index] = &array + (index * sizeof(int))
index[array] = &array + (index * sizeof(int))

This is possible because the compiler knows which refers to an address and which refers to an index, then plugs them into the equation regardless of the order presented.

SuperJimmyChanga
Автор

I knew this trick because it is a golfing technique, pretty cool.
I saw from people trying to reduce code size and instead of declaring the string, they did like "abcdefg"[3] or something. Then i noticed that there was a certain utility for this idea. I love this kind of freedom.

sophiacristina
Автор

Explained in head c first book beautifully

isfandyar
Автор

Another benefit of zero indexing is computing rectangular array indexes.

georgerogers
Автор

I never knew the official reason but i always found any time maths was involved, it made is so much easier, like mapping a multi-dimentional array into a linear array.

(RowNum * RowWidth) + ColNum

So in a 3x3 for example - [0-2][0-2], you can map it to a linear array [0-8] using 2 for loops. Then while it's stored in a linear array, you can still treat it like a 2D array using the above formula. So to reference the 2nd item in the 3rd row, it would be [(2 * 3) + 1] = [7]

marklonergan
Автор

A lot of things start with 0. Age, height, weight, voltage etc. Thinking about it, starting from 1 is really quite weird.

UniquePerspective
Автор

With 2[test] which data type/width is used? 2 as an implicit pointer, is it a void* so just 1 byte default width? If so, then 2[test] != test[2]. But pointer definitely cant be implicitly cast to int and vice versa. So I now actually remember how this works (total schizo comment), it literally is equivalent to *(left + right), and the addition expression inside obviously is commutative and int+pointer is defined and uses the pointer data type for width

Swedishnbkongu
Автор

Well I was going to start making C/C++ tutorial videos because I could never find any good videos in English but I guess I'm late to the party lol excellent content my guy. Clear, Concise, and easily digestible.

schoolForAnts
Автор

finally something that I remember from my intro to systems class

CrittingOut
Автор

This channel has an Over 9000 offset 🔥🔥

kirkkennedylincoln
Автор

Me an intellectual looping through the array in C:

int arr[3]={1, 2, 3};
int i=(int)NULL;
start_of_loop:
printf("%d\n", *(arr+i));
if(++i<3)
goto start_of_loop;

🗿😎

rukasuigh
Автор

For 2[test] to work, value 2 must be cast as (uchar*).

alpyre
Автор

Just a quick tip, you can do (sizeof *test) to make this work regardless of the type of the dereferenced pointer. That'll help prevent many many bugs (e.g. when you call malloc)

ToBeardOrNotToBeard
Автор

Ngl I thought it was because when you run a command + args the command is the first entry and the rest are args which is what you wanna use so they made it 0’index to incorporate the command (no research or proof of this, just my silly brain making assumptions lol)

happybroski
Автор

Don't care why, I am just very thankful

slnttt
welcome to shbcf.ru