you will never ask about pointers again after watching this video

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
one of the hardest things for new programmers to learn is pointers whether it's pointers by themselves pointers that point to arrays or pointers that point to pointers something about this concept just drives people crazy and if you're a new programmer well you're not alone i was one of those people when i learned c back in the day and like you i was eager to understand in this video i'll show you what a pointer is so you can fully understand how they work the syntax of pointers you can easily read them and finally why everyone cares so much about pointers and what they're used for before we start if you're new here hit that subscribe button and while you're at it leave a like i put out videos demystifying topics like this and much more on a weekly basis what is a pointer the question that's been asked since the beginning of time well maybe not that long but computer science students have been asking this question for a while pointers are not that complicated and let me show you why to understand what a pointer is we need to first understand how memory works so here i've laid out an example of memory memory in our example has two features an address and a value the address is the location of the memory meaning where that memory lives and the value of that memory is the data stored at that location meaning what memory lives there so for example if i put a 4 here what does that mean all that means is that the value 4 lives at location hex one thousand easy and the notation in c for example may be int x equals four which gets allocated to that memory on the stack and now that number lives there so what happens now if at another location i put the number hex 1000 at address hex 1004 i've just created a pointer you may be thinking low level learning how is this possible that's just a number at a location well guys that's the secret a pointer is just a value that happens to be an address mind blown by setting the value of one variable equal to the address of another that variable now points to the other for new programmers though most of the time that isn't as easy as it seems one of the most common issues new programmers have with pointers is the syntax used to create them the combination of stars and ampersands and arrows and more stars creates a lot of confusion so let's break this down using our previous example in our last example we made an integer x whose value was 4 at location hex 1 000 after that we made a pointer that lived at address hex 1004 whose value was hex one thousand so how do we do that in c we could do that using two lines of code and i'll break them down part by part the first line is int x equals four this line is pretty straightforward the first part integer is the type of the variable which is four bytes wide and this will matter later in the video the second part is the name nothing special here just the variable name x and then after that we put an equal sign which when describing c we can verbalize the equal sign to is set to and then finally the value 4. so the final sentence we've come up with is integer whose name is x is set to the value 4. okay easy part over next is the hard part to make a pointer to x like we did in our example i would say the following line of c int star px is equal to ampersand x now i know that sounds pretty crazy a little scary let's break it down piece by piece from left to right we can see the type again starting with int ah but next we see the dreaded asterisk what does that mean when an asterisk is placed next to a type it modifies the type meaning that our variable is now a pointer to an integer so our variable here points to a four byte value next the variable's name which is px or pointer to x you can name it whatever you want but this is a good habit using p to denote that it's a pointer and then after that our equals sign which again means is set to uh and then the next dreaded character the ampersand whenever you see an ampersand just think in your head the address of so this means the address of x our final sentence here is int pointer px is set to the address of x so what does this do for us well now by using the pointer we have a way of accessing x by reference instead of by value so for example if we wanted to copy the value of x to a new variable using that pointer we could do that pretty easily with this new bit of code we'll say that int y equals star px now what is this code doing let's break it down again why just like x is a normal integer so no pointers yet we say that y is set to using that equal sign uh-oh the asterisk again remember how last time i said when we see an asterisk it's used to modify a type if a type is near it well here there is no type when it's used alone this way the asterisk is a referred to as a d reference the d reference means go to the address pointed to by the pointer and grab that value so because px points to x the d reference will go and grab that value and it will set y equal to x so when we're verbalizing c when you see an asterisk by itself you can say the thing pointed to by this would mean that the final verbalization of this line of c is integer y is set to the thing pointed to by p x by doing this we can pass around x by reference instead of value and why that matters i'll explain in the next part of the video the final concept that confuses people the most when learning about pointers is why does anyone use them the syntax is confusing my programs crash all the time when i use them why does anyone bother well the answer is because we have to to avoid making code that is impossible to read or unscalable we break down functions based on the action that they perform so here i have a small snippet of c where i have a function that updates the age of a person structure the problem is that the structure i'm editing is not in scope of the editing function to get around this we passed the struct by reference so that now the pointer to the structure is in scope of update struct and can therefore be edited using pointers like this keeps our code clean and understandable while reducing the amount of space that we use by not copying another reason that pointers are inevitable when coding in c is the idea of using static versus dynamic memory allocation static allocation is typically a variable that goes onto the stack a place that is always in scope for the function that is running it however when you're using dynamic allocations that come from the heap through malik or s-break or other kinds of memory allocators you are going to get a pointer to memory that is out of scope if you ever want to be able to use this kind of memory you need to know how pointers work the primary difference between dynamic and static allocations that static allocations are things that are known to have a fixed size at compile time whereas dynamic allocations can be changed in size as the program runs here you see i allocate a string of 100 bytes to be pulled from the heap but that 100 bytes could have come from a user input or something else if you're having a hard time with c don't be discouraged pointers do take a minute to master but once you get them you'll know it and you'll feel like a real low level wizard when you do guys i had a fun time making this video if you enjoyed this do me a favor hit like hit subscribe and i'll see you next week take care
Info
Channel: Low Level Learning
Views: 2,085,027
Rating: undefined out of 5
Keywords: c pointers, c programming, c pointers tutorial, pointers tutorial, programming for beginners, programming language, programming in c, programming hero, programming fundamentals, programming memes
Id: 2ybLD6_2gKM
Channel Id: undefined
Length: 8min 3sec (483 seconds)
Published: Sun Jun 19 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.