you will never ask about pointer arithmetic after watching this video

preview_player
Показать описание
Understanding pointers and how to use them is a common problem for people learning new low level programming languages like C/C++. In this video, we dive deep into how pointer arithmetic works in C/C++. Specifically we talk about how the compiler uses the pointer type to do the math for you, making C/C++ pointer math easier than people make it out to be.

🔥🔥🔥 SOCIALS 🔥🔥🔥

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

I think the best way to explain this is that the pointer has a type, and that's why it multiplies it. If you were using a void pointer _then_ you would need to get sizeof(person), but since the pointer is declared with a type, you don't need to do anything else.

muha
Автор

Some issues I have with the example in this video:

- There is no need to use a pointer to iterate over an array. Just use array indexing!
- Assigning the address of an array to a pointer is incorrect. The array variable is a pointer to the first element of the array, so you should just assign the array variable to a pointer. This would also mitigate the warning that the compiler kept spitting out at you.
- The segfault most likely was because you kept walking off the array during the last iteration of the for loop. You were trying to access member data through the pointer past the bounds of the array.

With that said, you do a decent job explaining how to use a pointer to traverse an array. 👍

bettkitty
Автор

Dude, I sincerely did not understand a word of what you said, but I feel the need for learning C and low level programming. I just don't know why, but I have to do it, so I'll keep watching your channel until I learn. Thank you and keep it up.

vag
Автор

I haven't used C much and this is exactly how I expected it to be, just incrementing the offset with SIZEOF structure and was later confused about why.
Really pleased with your explanation.

raz
Автор

Programming C for a while, but 0:33 was the best explanation that I ever heard for using pointers! Thanks 😎
I use also always the python console for calculations or conversions 😂

maxhouseman
Автор

One thing that I can add to the "why use pointer?" Question is that pointers make you able to toss around arrays in and out of functions (read: as arguments and as return value). I know it has been said (briefly) in the beginning but I just want to stress this out as it might be what most newbies like me find out very useful.

rizalardiansyah
Автор

Pretty cool; for beginners, I believe line 13 might be confusing. I would have used "= people" or "= &people[0]"

AlessioSangalli
Автор

Nice video. Pointer magic has gotten better or worse (your choice ;-) in C over the years. I was an ASM programmer when I moved to C in the 80s, so pointers were normal for me. Back then I was talking to a lot of Mainframe Cobol programs and would marshal data back and forth between. There you had a lot of alignment issues you had to deal with and I had a fair bit of code to pad and un-pad and convert data structures. One of the great things about C is you have lots of rope. You just need to learn how to control it so as not to hurt yourself.

UReasonIt
Автор

You can also do, at the begining of the for loop
p_Person = &people[i];
Much more comfortable (even though your method isn't confusing either).

adamshufan
Автор

You make everything seem so easy! Thank you so much for your videos. You definitely make my life much easier.

itzkaleb
Автор

Thank you so much. I'm having great trouble picking up assembly language for the schoolwork. Much appreciated 🙏🏻

xdotli
Автор

Great explanation! Subscribed! I even understood your explanation/demonstration in assembly!!

thetooginator
Автор

4:00 i read a strlen implementation that increments an integer pointer by 1 which made me very confused. thank you for your explanation, this was very helpful

fatsu
Автор

Thanks for this wonderful tutorial! I like the highlight of incrementing pointer (let's say it's called pointer) by sizeof(*pointer) becomes incrementing the pointer by sizeof(*pointer) * sizeof(*pointer) bytes, instead of by sizeof(*pointer) bytes or one element away.

But let's not advertise for hungarian notation... Both the programmer and the compiler can keep track of the type themselves.
And it is worth to mention that the pointer to the first element of the array should be assigned as struct Person *person = people;, as warned by the compiler for incompatible pointer types, since it actually mean different thing in terms typing (people itself is already struct Person * when it decays to pointer; and adding an extra address of operator (&) in front makes it becomes struct Person (*)[100]: a pointer to "array of 100 struct Person elements" instead of a pointer to struct Person, what we want to access for each element in the array) but it just points to the same location for the array head case.
If we do &person + 1, it numerically returns &person[100] as a pointer to another struct Person array of 100 elements, which is out of bound, and hence causes UB.
If we have to be really extra explicit, we can write it this way: struct Person *person = &person[0];

leosin
Автор

Thanks for the refresher. I haven't looked at C in almost 10 years.

MichaelAbramo
Автор

If you want to move inside the struct (go to the next char in name for an example) simply cast the pointer into a char* then any arithmetic operation will jump a single byte and the += sizeof(struct person) will work when iterating along an array

zxopink
Автор

thankyou I was really struggling trying to use structs like objects, now I know how to use pointers :)

B_dev
Автор

Thank you! Starting my C class this week so I think I will be watching quite a few LLL videos

kailashbtw
Автор

If a person have programmed in Assembler only (due to starting programming in 1980's) when everything is just memory locations and offsets,
C just handles it for you with safe guards, though a (void*) can override mismatch pointer types if you know it's still compatible.

tonysofla
Автор

Thank You very much for making this video!!! - I was lacking that example. Sub added after I watched that.

mateuszpasternak