Тёмный

Master Pointers in C: 10X Your C Coding! 

Dave's Garage
Подписаться 695 тыс.
Просмотров 281 тыс.
50% 1

Pointer basics, indirection, void pointers, pointers to pointers, function pointers, callbacks and much more in this comprehensive introduction to pointers. For information on my book, "Secrets of the Autistic Millionaire": amzn.to/3diQILq
My other channel, join now so you're there for episode 01 of my AudioBook!
/ @davepl
Discord Chat w/ Myself and Subscribers: / discord
Primary Equipment (Amazon Affiliate Links):
* Camera: Sony FX-3 - amzn.to/3w31C0Z
* Camera Lens: 50mm F1.4 Art DG HSM - amzn.to/3kEnYk4
* Microphone: Electro Voice RE 320 - amzn.to/37gL65g
* Teleprompter: Glide Gear TMP 100 - amzn.to/3MN2nlA
* SD Cards: Sony TOUGH - amzn.to/38QZGR9
This is a revised edit (shorter and without intro) of the video from several days ago!
As always, all content and opinions are mine only, (c) 2022 Plummer's Software LLC.

Наука

Опубликовано:

 

16 июн 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 633   
@esra_erimez
@esra_erimez Год назад
My dad taught me about pointers using index registers in assembler.
@johnmckown1267
@johnmckown1267 Год назад
Sounds like IBM mainframe HLASM. I've been doing that for a little over 40 years. Although I vaguely remember some 8 bit computers which had an index register. I don't know x86.
@nezbrun872
@nezbrun872 Год назад
I used indirect addressing in assembler for some years before I realised I was using pointers, when I did a Pascal course around 1984.
@DavesGarage
@DavesGarage Год назад
I would have struggled less in the beginning if someone would have told me that a pointer is like 6502 zero page!
@toby9999
@toby9999 Год назад
​@@DavesGarageI programmed the 6502 way back in the 80s, but I didn't have an assembler. I did everything directly in hex op codes etc. But that stuff definitely helped me later with pointers in C i.e. they're just memory addresses as you said.
@vk3fbab
@vk3fbab Год назад
Dad's should teach assembly language to their children. I think of it like the Latin of computer languages. You learn about how the computer actually works and appreciate all of the things that come along for free with the HLLs that you will use. You also see pointers in things like the aptly named Stack Pointer and the Instruction Pointer. You also see function pointers in vector tables. Assembly is cumbersome but you learn so much by doing just a little bit.
@SonicBlueTyphoon
@SonicBlueTyphoon Месяц назад
These three sentences solved the big problem I was having when learning pointers: "Depending on the size and complexity of the house structure that could be an expensive and wasteful operation. Why is it wasteful? Well because just like in real life there's no reason to bring your house to the appraiser for an appraisal the appraiser comes to you." I was always so confused as to why you would use pointers when you could just carry the whole object around. But because it copies the entire house variable, which is often more data than a 8 byte pointer, this makes it efficient to use on any item that is larger than the size of a pointer. Thanks a ton!
@mossi5976
@mossi5976 9 месяцев назад
That analogy between memory address and the address of a house is great! I used to make similar comparisons when teaching C to students and it really seemed to help make it click for them.
@hookahtagen5421
@hookahtagen5421 6 месяцев назад
I'm 12 years into programming now... And i just understood the concept now, with that analoy :D
@lukas-kevynmuller6697
@lukas-kevynmuller6697 4 месяца назад
as if you never heard that analogy before. that analogy was in every tutorial I've seen.
@greg4367
@greg4367 Год назад
Dave, i have been coding in C since 1980 and have, many times, tried with variable success to explain pointers to newbies. This video is clearer and more concise then my best efforts. Going forward I'm just going to refer newbies to this. Thanks.
@DavesGarage
@DavesGarage Год назад
Hope it helps others!
@saberlite8809
@saberlite8809 Год назад
You look exactly like a person who would teach me about pointers. Just like a Wizard NPC in games.
@itsSpidey-
@itsSpidey- 8 дней назад
What kind of statement is this? 😂
@fedep.6741
@fedep.6741 3 дня назад
@@itsSpidey- he is saying that dave's appearance exudes competence and advanced knowledge of the subject
@tangiblewaves3581
@tangiblewaves3581 Год назад
Hi Dave, I discovered your channel a few days ago and enjoyed some windows related vids; today I wanted to improve my understanding of C pointers and, voila, here you are again! I looked at some other tutorials, but yours is definitely the best! I really love your style! Keep up your great work; it's pure gold! ❤
@johannesschneider1784
@johannesschneider1784 Год назад
This is the most amazingly coherent, clear and informative explanation of pointers I have seen in 14 years since I started programming in C and C++. This is beyond fantastic omg please continue. :)
@connecticutaggie
@connecticutaggie Год назад
Wow, 40 years ago I was still programming Fortran and had never heard of object oriented programming. At the time I was designing a production program to run various tests on pacemakers and since all the tests had some shared behavior (like limits checking, saving results to a database, printing results, etc.), I decided to create a large common block array that was indexed by something I called a "test number" then functions could do what they needed with just a test number. One huge advantage was that I could configure what tests to run and what limits to use and units to report with a big constant array at the top of the code. This meant that when a new test specification came out, usually all I had to do was to change the constant array and no code change was required. Later on in my career my SW boss told me I had created Object Oriented Fortran. Interesting - I was just a young "lazy" programmer trying to find a way to do more with less code to develop, debug, and validate. Also, being ADD, my brain worked faster than my fingers.
@tangiblewaves3581
@tangiblewaves3581 Год назад
Haha, all programmers are lazy; this is what drives the development of technologies, languages and so on!
@theMaizeOfficial
@theMaizeOfficial 11 месяцев назад
@@tangiblewaves3581 well i agree but i dont agree, i think we always find shorter ways to do things and yes we develop wicked tech from it, but programming its self can be very intensive / labor intensive. And takes a lat of work even to learn, so i get what your saying, but programmers are just like any trade (plumbers, electricians) and just like those trades look for better ways. I have a family full of trades, and they are full of unique (what i would call lazy) ways to fix stuff but it works and they have good solid reputations for the work they do, sometimes its those ways that make a completely new product that works 10x what the previous method is, but it takes knowledge and the skill to think it through (and sometimes the balls to apply the fix lol and hope it works.) just like them, programmers, designers and scientist need to sometimes "go back to simple" in order to step forward. All that said... i have seen lazy programmers and lazy plumbers. so i think the ratio is likely the same in terms of avoidance of physical labor. o7
@OneMilian
@OneMilian 10 месяцев назад
@@theMaizeOfficial exactly, and if you want to code proffessional, you also need some good amount of discipline to code even when you have a bad day or are tired.
@atlantic_love
@atlantic_love 7 месяцев назад
Cool story.
@elpatosilva
@elpatosilva 4 месяца назад
A good programmer is that who will spend 3 hour of his time to automate a one time task that requires half an hour to be made manually.
@JamesLatimer
@JamesLatimer Год назад
Wow. This made a whole lot more sense to me by the end. Thanks Dave!
@yoshi4980
@yoshi4980 Год назад
i was a TA for an intro to C class, and the house example was what i used to describe pointers to people really confused about them. glad to see that you are using a similar example here! i feel that the easiest way to describe technical concepts to people who are confused about them(or have no technical experience/knowledge) is to use a real world analogy, even if it oversimplifies what is actually going on. once they understand the analogy and how it relates to the technical concept, then they can get to the nitty gritty of understanding the technical details behind the concept
@Evilive15
@Evilive15 11 месяцев назад
Such a good video Dave, straight to the point. much love from Argentina!
@morrick
@morrick Год назад
I have always struggled with this. Dave, your explanation is the best I've ever heard and it worked for me! Like you say "it clicked"!
@thegame4027
@thegame4027 Год назад
Its a great video if you understand pointers but it fails to do the most important part of why people don't understand pointers. People don't understand pointers because they don't understand the problem they are solving. The moment you understand how memory works, how data is stored/loaded in/from memory and what the cpu has to do differently depending on if you call by reference or by value is the moment you get pointers. The real explanation needed is not what pointers are but what problem they solve and why you need them. Back when i was learning C i had a hard time wrapping my head around pointers until one day my boss took an houre to explain the difference. He showed me the assembler output of different scenarios with and without pointers and explained what happened on each line inside the cpu and memory and it just all made sense after that. It's not about what pointers are but what problem they solve and how.
@vcv6560
@vcv6560 Год назад
Assembly is a lost art, but explains so much.
@lukas-kevynmuller6697
@lukas-kevynmuller6697 4 месяца назад
this
@mahee96
@mahee96 3 месяца назад
Yeah assembly could have explained it simply.. You have Registers, and direct/indirect addressing… all registers can be pointers unless they all are general purpose registers Then the addressing and instructions decide how we are representing the registers LA R1,=A(0000) Load address of location where =A(0000) has been resolved in the executable, into Register 1 XC 4(256, R1), 4(R1) XOR 256 bytes starting from values pointed by R1 offset by +4 bytes with the same… (XOR of itself is 0 ie clearing all the bits/bytes at that location) ST R1,INT32 Store value in R1 into location INT32 L R3,INT32 Load value at INT32 into R3 LA R4,INT32 Load absolute address of symbol INT32 into R4 ST R3, 0(R4) Store R3 into INT32 symbol location INT32 DS 4F Someone who knows assembly should be able to teach pointers in a better way
@TheFeralEngineer
@TheFeralEngineer Год назад
I've been waiting for you to dive into programming like this... Did not disappoint. Bravo 👏👏👏👏
@jimwinchester339
@jimwinchester339 Год назад
Good video, Dave. I also have been working in C/C++ for about 40 years, including work for some notable compiler vendors such as C86, Zortech C++, and GCC (via Cygnus). This was IMO an uncommonly clear and well-organized presentation. Was impressed that you had time to include pointer-to-functions in this 14-minute overview. Perhaps you might have mentioned the precise prototype matching required to do real-world useful work involving pointer-to-functions; and in particular, how judicious use usually warrants declaring a typedef for a particular group of related pointers-to-functions.
@scottcooper3107
@scottcooper3107 Год назад
Thanks! Very Helpful. You explained some things that C++ books that I have read have overlooked. Like ..... The reason why we even need to use pointers to begin with. I hope to see more content like this.
@arf9759
@arf9759 25 дней назад
Hands down one of the best and concise explanations of pointers! It seems to all click in place after using and avoiding them all these years!!!
@possumcode
@possumcode Год назад
Let me speak from my heart =) This description was absolutely incredible! I wish I've had it about 5 years ago..
@AusSkiller
@AusSkiller Год назад
Pointers were so logical and intuitive for me. When I was first learning C++ I was coming from having played around with languages that didn't have dynamic allocation, I didn't know what dynamic allocation was at the time but I had been frustrated in numerous projects I had made that I couldn't make new variables at runtime. So when I first read an example of malloc and using a pointer I immediately put down the book (before even reading the explanation of the example) because I had recognized it as the solution to what I had wanted for so long and wrote a linked list (not that I knew that it was a linked list at the time) to verify that it could do exactly what I had hoped it could, and it worked. I was so excited, the possibilities of pointers were limitless in my eyes and I messed around with them for most of the rest of the day. When I finally got back to reading the book that I was using to learn C++ I was a little disappointed to learn that everything I thought I had created to use pointers for was pretty much covered in the next chapter, but I was glad that at least I had understood them well enough that I could correctly extrapolate their uses without knowing much more than that they existed. It also turned out the example was only the first half of the program that only covered allocation, it wasn't until I turned the page to see the deallocation example and read the explanation for it that I realized all the horrors of memory leaks and how many I must have created that day 😉
@IBelieveInCode
@IBelieveInCode Год назад
"having played around with languages that didn't have dynamic allocation" Cobol ?
@AusSkiller
@AusSkiller Год назад
@@IBelieveInCode Logo and Visual Basic, though I was very young at the time and no internet so I might just not have been aware if they actually did have that capability.
@TehIdiotOne
@TehIdiotOne Год назад
Honestly i don't have any problems at all understanding the concept of pointers, all that makes sense to me. What i don't understand, is when you want to use them
@bkw777
@bkw777 8 месяцев назад
Maybe that is the right way to tech the concept, is to pose problems that require the indirection to do the job. A lot of things are easy if the person thinks it was their own idea or something they want rather than something imposed from outside. Get them to say for themselves "what I really need is some way to ..."
@soooslaaal8204
@soooslaaal8204 6 месяцев назад
@@TehIdiotOne as said on this video, the most common use is with larger or variable data types like objects, it is much more efficient to point to the adress of an object than bring the entire object into memory, this is especially noteworthy in C++ when storing lists of large objects or integers, you just use the pointer and the -> operator and it is way more memory efficient. Plus, it allows for dynamic memory allocation which allows to manage what is and what isn't in memory at any given time
@fastestdraw
@fastestdraw Год назад
This is really, really helpful. You have a talent for explaining things.
@andtpfack8243
@andtpfack8243 Год назад
Great video. Straight to the point, easy to follow along.
@ochbad
@ochbad Год назад
Thanks for making this video! I still remember the exact ‘ahah’ moment (two decades ago in freshman cs) when I grokked c memory (pointers, arrays, structures). Learning how c handles memory really helped me understand how computers work.
@fgelana
@fgelana Год назад
Thanks Dave!! I am learning C++ and pointers where the most challenging part. This was a great help.
@alrutto
@alrutto 10 месяцев назад
Thanks for this, Dave. I led the way you explained and the examples. I hope this C/C++ videos keep coming :)
@kisungyou6505
@kisungyou6505 11 месяцев назад
Your video encouraged me to Go Pointers again! Thanks!
@jeffsmith1798
@jeffsmith1798 6 месяцев назад
Thanks for the pointers, Dave!
@MyCodingDiarie
@MyCodingDiarie Год назад
This video is exactly what I was looking for to help me improve my programming skills. Thank you for making it!⭐♥
@TheIllerX
@TheIllerX 6 месяцев назад
Very clear introduction to the most important things about pointers. Remember struggling a lot with those when I started using C++.
@andreipriboi8373
@andreipriboi8373 Год назад
one of the best videos on this topic in my opinion. thank you!
@derekchristenson5711
@derekchristenson5711 11 месяцев назад
That's a very nice refresher on pointers. Thank you!
@Horendus123
@Horendus123 11 месяцев назад
Thank you so much for putting together such a great video on pointers. I’ve watched it twice and I plan to watch it three or four more times until I understand every single concept of gone over here. The function pointers are still a mystery to me how I would use those affectively
@user-wt7pq5qc2q
@user-wt7pq5qc2q Год назад
Awesome !! I now understand pointers better than I ever had. Thanks
@connorhillen
@connorhillen 11 месяцев назад
I recently started teaching the introduction system's programming course at my university, and this should make for a great review resource going forward, thank you for this and the many great videos!
@rahulbeniwal6321
@rahulbeniwal6321 7 месяцев назад
Thanks Dave for revising my C concept. Last time when I did was solving exercise of K&R in 1 st year of graduation. Clap for your line "I am not here to sell anything".
@mathiasdreke180
@mathiasdreke180 Год назад
Thanks for your programming content! I have been a programmer for a while but nowadays information technology seems much too complex for me. I cannot code for real but I can follow your explanations and understand them. It's just I have problems memorizing them. So I consider your videos a source of sophisticated entertainment.
@freddybronson5410
@freddybronson5410 3 месяца назад
Absolutely fascinating video!
@Upsilon-gb6ch
@Upsilon-gb6ch 5 месяцев назад
very good explanation. especially the function pointer part.
@mrcrackerist
@mrcrackerist Год назад
You can also subtract a end pointer from a start pointer to get the length of data or string. As for void pointer if you use the same format for all your structs then a uin64_t in the beginning can be used to identify the data type.
@onejdc
@onejdc Год назад
Two follow-up tips: The & (address-of operator) is used when you want to turn the address value of something into a pointer. Second, you will sometimes see that asterisk * in different places between the type and the pointer name: e.g. int* a; int * a; int *a; int (*a); ... all of these are exactly the same. The whitespace doesn't mean anything in these examples. That took me forever to understand. Just try to be consistent in code.
@JamieT1986
@JamieT1986 Год назад
int* should be the only way to write int pointer.. everything else should be banned 😄😄 To me it makes the most sense. The type is an "int pointer" so put it with the type name... I think it's confusing for beginners because it looks similar to the dereferencing syntax.
@vaakdemandante8772
@vaakdemandante8772 Год назад
@@JamieT1986 yes, but then a declaration in 4:07 would create 2 pointers, not one. int * x, y is in actuality: int *x AND int y. I agree it would be better to just have something like 'intP x, int y' and then it would be obvious what is a pointer and what isn't but we got stuck with a syntax that is efficient for writing, but hard for reading - somewhat opposite to Python's syntax.
@JamieT1986
@JamieT1986 Год назад
@@vaakdemandante8772 int* x, y; is something I was a little skeptical about actually when I saw this. I never knew that's how it worked, and I forgot to test it myself. I will assume it's correct... but yeah that kinda screws it up! :)
@jeromearenas8028
@jeromearenas8028 Год назад
"int \*num" is much better, the declaration in 4:07 is confusing if you use the "type\* var" version. Edit: I used "\" sign cuz youtube comments converts asterisk to bold letters. lol
@tristennollman5344
@tristennollman5344 Год назад
This^^^ i would say, find a style and stick to it, but as an OS and firmware dev the most common style i come across (and use personally) is this: Type* var_name = ….whatever here…. Ex: int* pointer_to_integer = (int*)malloc(sizeof(int)); So the asterisk essentially lives right next to the type, with no whitespace between. This makes it clear that it is a POINTER of said type, in my example a pointer to an ‘int’ Using this style consistently makes reading and reasoning about pointers much less frustrating in my opinion. Of course, then there is dereferencing a pointer to get the object’s value. In those cases common style i see and use is this, continuing with my int example: int integer_variable = *pointer_to_integer; So we use the asterisk right against the VARIABLE NAME to dereference it. The combination of sticking to these two basic style guidelines makes points way less messy. Essentially, use the asterisk directly next to your type name when declaring a pointer variable, and right next to the variable name when de-referencing it. Hope this helps some of the less experienced! Also, as a follow up, i think the example at 4:07 of Int* x, y Is a kind of poor example. It just isnt code that would fly in most (well maintained) C projects like the linux kernel for example. You want things to be CLEAR above all else, and so its not really much effort to be explicit with each variable declaration. The compiler can optimize for you if it is necessary or possible. Otherwise, be more verbose in an effort for future contributors to have an easier time grasping what your code is doing. Little syntax tricks like that can be neat, but often fall flat in well maintained project environments with C.
@waynec369
@waynec369 Год назад
Thanks, Dave! This is what I'm talking about! Coverage of more advanced C/Cpp topics. Please keep up with explaining some applications where the topic-of-the-day is used. I will say again, your knowledge should be memorized in a book. I would buy two!
@potatoplays2
@potatoplays2 Год назад
Thank you for such a clear explanation I have a project coming up for implementing data types so this really helps
@suvetar
@suvetar 7 месяцев назад
Man, I could have used such a good explanation 30 years ago!! Thank you Dave!
@bank8489
@bank8489 2 месяца назад
THIS. this is the video that FINALLY made it click for me. thank you Dave
@Pariatech
@Pariatech Год назад
Starting with C and fiddling with memory made understanding how more higher level language handle memory / what is passed by reference vs value.
@cyndicorinne
@cyndicorinne Год назад
30 years ago I learned about pointers in C, and I’m grateful for this recap. Some things made more sense even now, after watching this, particularly the double indirection.
@PianoJoe822
@PianoJoe822 7 месяцев назад
Very helpful video, thank you!
@thiagobaptista
@thiagobaptista 11 дней назад
The very first programming language I learned, back in 1995 when I was 11 years old, was C. I had to tackle pointers from the get go, and of course they were a difficult topic for a child to grasp initially. But it made a HUGE difference for me when, years later, I started learning more high level languages such as Java, C#, and Ruby. I was WAY ahead of my colleagues who were learning those as their first programming languages, as the internals of what we were doing were much more intuitive for me.
@manfredrichtoften8848
@manfredrichtoften8848 2 месяца назад
Holy mackrel I don't think I ever rewatched a single video consequtivelt as this. Sir, in 12 minutes, you explained pointers better than my professor in an hour and a half.
@cd860viu
@cd860viu Год назад
I've been (slowly) learning C as my starting language just for fun. Pointers has given me many headaches and I still can't completely get a picture in my head of how they work. I wish sometimes that they had a special name or different symbol, maybe that would've made it easier. But you mentioned a few things (that I had to listen to 3-4 times) that gave me some better insight, so maybe one day. Thanks!
@olafzijnbuis
@olafzijnbuis Год назад
Making a drawing of it often helps a lot.
@bob_mosavo
@bob_mosavo Год назад
Hang in there! You'll understand them 👍
@newmonengineering
@newmonengineering Год назад
C and C++ learning is like this: you learn the basics and start writing. Then you want to do more or understand someone else's code, at that point you go through 3 to 6 months of FOG where you don't quite get it, but you keep trying. The good news is once you get past that, you become a decent programmer.
@cd860viu
@cd860viu Год назад
@@newmonengineering I basically set out to make a reasonably simple game and by using reference code from others or books I've managed to piece things together bit by bit. By interpreting compiler errors I've managed to kind of get a feeling for when it's in need of a pointer or not. So I definitely know more now than half a year ago luckily :)
@morgengabe1
@morgengabe1 Год назад
I think C started a bad pattern for working with pointers because you declare one with the same symbol you use to dereference one. That said the arrow operator has some, as of yet untapped, potential for conveying ownership semantics needed for automated resource management (at the cost of the control C devs freak over :p)
@ElliotAlderson835
@ElliotAlderson835 10 месяцев назад
That was so good and so well explained 👏❤
@patrickstival6179
@patrickstival6179 Год назад
Nice one. I found this video easy to grasp
@arondsouza3568
@arondsouza3568 5 месяцев назад
great explanation mr. dave
@robertbruce7686
@robertbruce7686 Год назад
Excellent! My best saga was creating a pointer to an array of functions (oh what fun) 😆
@vcv6560
@vcv6560 Год назад
My experience with pointers came with my first programming being PDP-11 assembly (school) and later 6502. So dealing with them in 'C was very straightforward...after the syntax of course. I say that because your comment about 'decorating the variable' rather than the type, (int* x is not int *x) was my personal stumbling block.
@eramires
@eramires 8 месяцев назад
Thanks for this, I always had pointers as something nasty but this video helped me. :D
@StaticBlaster
@StaticBlaster 5 месяцев назад
Thanks for the pointers.
@juliusfucik4011
@juliusfucik4011 Год назад
I started learning Turbo Pascal in the mid 90s, so for me pointers where never really hard to grasp as TP uses them too. Once you have the concept in your head a pointer to an array of pointers becomes a natural thing to work with. Great video!
@fadichamieh
@fadichamieh Год назад
We are that old, eh?
@nezbrun872
@nezbrun872 Год назад
I first used pointers in Pascal on a mini computer in the mid 80s, and then Turbo Pascal in 1986, although I was also an assembler programmer before that, where you use pointers all the time but call it indirect addressing. Pascal taught me about structures. Then I went onto C, but it was K&R, and I found the nomenclature hard to follow: it took me months to get proficient, not because I didn't understand the concepts, but because K&R C can be super impenetrable for a C noob. Thankfully ANSI C came along at just the right time, I re-wrote the K&R code I was maintaining in ANSI, and I've stuck with that ever since. C remains my language of choice to this day, but mostly I'm a deeply embedded systems programmer and mixed signal hardware engineer, where C sits well, nice and close to the hardware. I dip into assembler still every now and again for those pinch points, but 99% is C.
@johanloubser8138
@johanloubser8138 Год назад
​@nezbrun872 I literally use Delphi for fun nowadays, with a free Community license. It's pretty fun tbh
@buttforce4208
@buttforce4208 Год назад
Love this, thanks Dave!
@SimonWahlin
@SimonWahlin 9 месяцев назад
There's an audio book coming? Wow! Can't wait! Have the physical book and love it so far, but finding time and mood for sitting down to read on paper is hard.
@joshismyhandle
@joshismyhandle Год назад
Thanks Dave, helpful stuff
@theseenandunseen
@theseenandunseen Год назад
great video; keep them coming
@InterestingTed
@InterestingTed 4 месяца назад
This is very helpful. Thank you!
@subhranshudas8862
@subhranshudas8862 Год назад
Feels like a PBS documentary. love it.
@outsideworld76
@outsideworld76 6 месяцев назад
Picked up writing some code for microcontrollers, my C is a bit rusty so these video's are very welcome.
@Sysshad
@Sysshad Год назад
Very good tutorial which needs to be reminded now and then and again :D
@DavesGarage
@DavesGarage Год назад
Glad it was helpful!
@LiviuGelea
@LiviuGelea Год назад
I learnt about pointers in high school. Having programmed in a higher level language for the next decade or so I really never needed them except for knowing they’re really what's used under the hood for all the syntactic sugar I grew accustomed to. What clicked for me was when, trained to use polymorphism I tried to obtain the same result in c++ embedded only to realize you cannot send daya structes of different sizes to same function. Using pointers was the solution for passing different implementations of an interface of varying byte sizes.
@SandorPipei
@SandorPipei Год назад
5:41 The -> pointer "dereference operator" is the shorthand variant for (*struct_name).member e.g. (*pHouse).SquareFeet 11:00 Usually when we program in C without classes ,we are using malloc. In C with classes (C++) we use new. Syntactically are both correct but new is safer. 2:58 Hardware registers or memory locations (physical memory locations on a CPU without MPU/MMU) can be accessed using a single line of code like *(byte *)53281 = 0x00;
@nezbrun872
@nezbrun872 Год назад
"Usually when we program in C without classes ,we are using malloc" It depends on the platform and application: in embedded microcontroller applications, which are typically memory constrained. would fail catastrophically in the event of failure, so malloc is rarely used in production hardware. As a result, memory and objects are typically allocated statically, or on the stack (typically small objects only on the stack).
@danielrhouck
@danielrhouck Год назад
Calling C++ “C with Classes” is sort of like calling Firefox “Netscape Navigator”. Or maybe like calling Windows “MS-DOS 1.0”.
@JohnDoe-kh3hy
@JohnDoe-kh3hy Год назад
@@danielrhouck It is something common among C (ansi, K&R, 90, etc) fans.
@danielrhouck
@danielrhouck Год назад
@@JohnDoe-kh3hy Classes are one of the *many* things C++ does differently than C, and they are *not* the most important one to this discussion.
@toby9999
@toby9999 Год назад
​@@danielrhouckNot really. Stroustrup referred to C++ as "C with classes". If it's good enough for him, it's good enough for me. It's a nice succinct way to describe the language conceptually.
@FabricioRWitt
@FabricioRWitt 7 месяцев назад
Legendary explanation.
@kencreten7308
@kencreten7308 Год назад
Master Pointers! I've been waiting for him!
@radosmirkovic8371
@radosmirkovic8371 3 месяца назад
Excellent tutorial
@andrewsmilie140
@andrewsmilie140 Год назад
On the first example, it is important to note the memory address needs to be casted to a byte pointer. When I was learning, this confused me a bit, especially when working with custom types. You can have an entire buffer filled with data which looks random, but if you cast it to the correct type when dereferencing, all values in the custom structure are magically decoded, and it is extremely fast!
@DavesGarage
@DavesGarage Год назад
You're right, I remember "struct IntuitionBase * pIntuitionBase = (struct IntuitionBase *) LoadLibrary(:...)" is what threw me the first time!
@Felice_Enellen
@Felice_Enellen Год назад
@@DavesGarage We really needed the modern version of the auto keyword back on the Amiga. 80 columns only go so far.
@llothar68
@llothar68 Год назад
Because i guess you did not start with assembly langauge. This was my 2nd language after Basic .... like all teenagers in the early 1980s home computer time. Feeling sad for the current generation that think it's not only magic but black magic.
@andrewsmilie140
@andrewsmilie140 Год назад
@@llothar68 actually, in my Electrical Engineering curriculum in 1999 I took a class in ANSI C in Unix and an Assembly class. In the Assembly class I wrote a 8088 CPU emulator, which was both frustrating and fun! I ended up using assembly programming PIC microcontrollers more than anything else. I used the term “magic” because I know what it is doing under the hood for me, and how much effort the C language saves us rather than writing in pure assembly! Cheers! Edit: it was the 8080, not the 8088-long time ago and I have lost the floppy disk where I had a backup. Oh well…. My degree is in Electrical and Computer Engineering (ECE) and I took quite a bit of digital design classes, focusing more on hardware than software.
@jordixboy
@jordixboy Год назад
@@llothar68 im self taugh software engineer, i did not went to cs, and I know this stuff, cause I love digging deep, no need to feel sorry. most of these jobs doesnt require this knowledge, yo ucan still learn it as hobby
@frankdearr2772
@frankdearr2772 3 месяца назад
Great topic, thanks 👍
@seanmccrary8300
@seanmccrary8300 Месяц назад
Dave, I've been watching your channel for a while now, you might not read this, but I found this video very interesting. I started to learn to code several years ago, and got stuck in tutorial hell in several language. C is the first one after many years I've actually stuck with. I guess my brain is wired different, so the features of a lot of the new languages have too much abstraction for me. I like to know how things actually work, and C has been a huge stepping stone for me to finally get into software engineering. It's been more of a hobby for me, but I would like to do something involved in it whenever I retire from the military God willing. I understand hardware very well from my background of troubleshooting, and working on weapon systems that employed digital electronics, radar theory, signal data conversion etc, but programming was always a mystery for me, it sounds stupid I guess, if I can grasp hardware, software should be easy. Thankfully C exists. My question to someone with your background, is there a good method of connecting with people other than reddit, that could help with pointing to resources to expand my knowledge on C and the data structures/algorithms that are needed to be learned in the industry?
@AnExPor
@AnExPor 3 месяца назад
I love function pointers. I thought that was the coolest shit when I was studying CS in college.
@Danielle-ew1el
@Danielle-ew1el Месяц назад
this video deserves more views!
@EdwinB-di1ji
@EdwinB-di1ji 20 дней назад
Thank you for the video. Please make a complete tutorial of C and C++. You have TON of knowledge and experience I would love to learn from you.
@siadat
@siadat 4 месяца назад
The Commodore 64 screen background colour byte example was brilliant!
@malectric
@malectric 9 месяцев назад
I got my first taste of this type of pointer use (outside of assembly language) when I had to learn Modula-2. Thankfully it was a realtively small leap from there to using them effectively in C but the pointer declaration and use nomenclature in C was a bit confusing. This was very helpful thanks.
@sjfsr
@sjfsr 10 месяцев назад
Great explanation.
@komivalentine3067
@komivalentine3067 Год назад
Thank you this really helps me get started with C++ ❤
@muddyexport5639
@muddyexport5639 Год назад
Excellent! I am coming to C++ and Rust from other linguages both compiled/assembled (primary) or interperted. (50+ years in programming -- livelyhood/ hobby to retired and just hobby). If this is a topic that should be explained: o Coding: Best Practices, with emphasis on: - Readability via Logical Format (after or along with the comments to explain) - Preventing Memory Leaks > Tools Available To Check - if such a thing exists Links to, if this is something best explained by examining what you consider good coding practices. Programming helps keep my mind sharp. Plus I just love it. When working I got paid for what I would have done for free (well almost). Looked up and it was time to go home. Couldn't wait to get back to where I left off. Thanks again,
@NevosLP
@NevosLP 11 месяцев назад
This is actually pretty good. Until recently I had also been learning C and while it takes quite a bit longer to become proficient in it than I have, this talk about many pitfalls in that many beginners in that language fall into. While it's not the most convenient language to learn, it's still really useful in giving you an idea about how certain parts of computers work and hopefully let you learn higher level languages as well. I tend to agree that a bottom up learning pattern is better than bottom down, so even if you're not too interested in C, it's still a useful language to know, at least generally, even if you're not proficient in it. A lot of things in Programming are still based on it and it would just be weird if a proper programmer didn't know certain basic things about programming that you learn in C, even if you don't directly need it these days. Keep ad it, I struggled at first too but once you get the hang of it, you'll wonder how you ever had troubles with it. If you're into it, programming can be a great thing, especially for people that like more practical uses of math but also many people in general. It's not nearly as rigid and a lot more creative than many like to think. So even if someone isn't that much into math, it can still be pretty nice to learn and use. Solving some of those problems you'll end up encountering, requires often a lot more than just purely logical, mathematical thinking, even if tends to be the basis of it. There's lots of creativitiy to be found in programming.
@BogdanSerban
@BogdanSerban Год назад
I understood the point of pointers when I tried to access registers in a microcontroller. And to this day it makes the most sense of using them.
@guilherme5094
@guilherme5094 Год назад
👍👍Thanks Dave!
@gregledbetter5942
@gregledbetter5942 10 месяцев назад
This was good for me thank you Dave
@angelshtilianov4767
@angelshtilianov4767 Год назад
Wow! Dave, I hope to get your permission to use that video to introduce pointers to a lot of people, or to introduce a lot of people to the pointers ;-) Its painfully necessary these days!
@generovinsky
@generovinsky 11 месяцев назад
thanks for the pointers dave ;P
@mr_noodler
@mr_noodler Год назад
Beautiful video I enjoyed it a lot
@POVShotgun
@POVShotgun Год назад
Damn for some reason I couldn’t pick this up in university but the way you’re explaining it finally makes sense lol.
@Nathan00at78Uuiu
@Nathan00at78Uuiu Год назад
Liked the parts where you shared why we would want to use pointers. Like the malloc api context was good to learn about.
@vcv6560
@vcv6560 Год назад
Also too, (kept in my mind) the stack on 6502 is so small you could never pass a struct like item to a function with anything but a pointer.
@sjoervanderploeg4340
@sjoervanderploeg4340 Год назад
I love these C episodes!
@TheEvertw
@TheEvertw 5 месяцев назад
I made the jump to C from Assembly, somewhere in the eighties. Pointers are trivially easy to understand if you know assembly.
@thomaslindell5448
@thomaslindell5448 4 месяца назад
Thanks this was easy to understand
@drewwilson8756
@drewwilson8756 Год назад
We who are about to design and build salute you!
@johnkuehler2608
@johnkuehler2608 5 месяцев назад
Very succint and nice . Like K&R C. I had used function pointers for years but never understood the int (*f) (arg) why u need the parens around the f. I do now. thanks.
@SpinStar1956
@SpinStar1956 Год назад
Even though I come from TTL and then Assembly, I’ve always had inordinate amounts of trouble with symbology involving assembler-directives, figuring out other peoples UIs and pointer notation. 😢 Maybe I’ve been living on the ‘Spectrum’ all along 😂 Thanks for the video and clearing up some of the confusion with the notation! 😊
@MyMy-tv7fd
@MyMy-tv7fd Год назад
good stuff Dave
@MrMalchore
@MrMalchore Год назад
I once had to create an array of function pointers. I remember nearly going blind with the syntactic gynastics that was required to create the thing and actually use it without leading to memory leaks. That was some 30 years ago, when I was programming embedded systems. Anyways, I strongly agree that mastering pointer syntax is important to improve your code READING skill. Oh, yah, and I heavily commented all that code. I typcially had more comments in my code then code, but I recall going way overboard commenting that code.
@me0262
@me0262 11 месяцев назад
Free the mallocs! Awesome video. Pointers are always misunderstood and often not done properly, they get especially finicky when you start using classes and C++ conventions, like the Trilogy of Evil (Copy, Assignment, Destructor operators) where data inside pointers has to be handled, not just the pointers themselves. One thing I came across was that in classes, you can allocate memory space with malloc, but it will not call the constructor for you. Another thing I've come across a lot when debugging is that people will call delete on the pointer when it's freed, but you also have to set the pointer to null in order to not leak that last bit of memory. One thing that needs to be taught is how to clean up after your allocations (free() and delete/delete[]). I never learned this from three separate classes, until I went for a Game Development degree at Full Sail.
@9adamlevin
@9adamlevin Год назад
6 years of college working in C++ and i had never even heard about function pointers, thanks for showing me something i didn't even think was possible!
@Hennue
@Hennue 11 месяцев назад
Function pointers are kindof the origin of object oriented programming. You would create a struct with function pointers as member variables. Then a "constructor-like" function could initialize the struct with the desired functions and voilá you have a class. C++ started out as a transpiler to C and, I might be wrong on this part, that is how i think it translated c++ classes into c structs.
@PiotrPilinko
@PiotrPilinko 2 месяца назад
Function pointers are currently almost not used (directly) in modern c++ (of course they exist and can be used if needed): most of the functionality was moved into lamba/function objects which is more robust and powerful solution (with some small problems). Also operators new/delete should not be directly used (as smart pointers are better option with make_shared and make_unique templates).
Далее
heavy boot #tiktok
00:16
Просмотров 829 тыс.
СКОЛЬКО МОИХ ТРЕКОВ ТЫ ЗНАЕШЬ?
00:41
Modern C++: Unique Ptrs and Vectors
16:25
Просмотров 133 тыс.
11 Characters That Crash Any PC: the Fork Bomb!
11:59
Просмотров 498 тыс.
WHY did this C++ code FAIL?
38:10
Просмотров 156 тыс.
What are double pointers in C?
14:43
Просмотров 41 тыс.
E01: Stupid C++ Tricks with Dave
15:43
Просмотров 322 тыс.
Harder Than It Seems? 5 Minute Timer in C++
20:10
Просмотров 133 тыс.
Modern C++: Upgrade Your Skills with Shared Pointers!
17:43
C++ Super Optimization: 1000X Faster
15:33
Просмотров 296 тыс.
сюрприз
1:00
Просмотров 1,3 млн
keren sih #iphone #apple
0:16
Просмотров 1,7 млн