YTread Logo
YTread Logo

you will never ask about pointers again after watching this video

Mar 19, 2024
One of the hardest things for new programmers to learn is

pointers

, whether they are

pointers

by themselves, pointers that point to arrays, or pointers that point to pointers. Something about

this

concept drives people crazy, and if you're a new programmer, you're not alone. I was one of those people when I learned c in the past and, like you, I was eager to understand it. In

this

video

I

will

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 are used for before we start, if you are new here, hit the subscribe button and while you're at it, leave a like.
you will never ask about pointers again after watching this video
I published

video

s that demystify topics like this and much more. more weekly what is a pointer, the question that has been asked since the beginning of time, well maybe not so much, but computer science students have been asking this question for a while, pointers are not that complicated and let me show you why understand what. One tip is that we must first understand how memory works, so here I have presented an example of memory. In our example, memory has two characteristics: 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 in that location which means what memory lives there, for example if I put a 4 here what does that mean?
you will never ask about pointers again after watching this video

More Interesting Facts About,

you will never ask about pointers again after watching this video...

All it means is that the value 4 lives in the hexadecimal location thousand easy and the notation in c, for example. can be int I just created a pointer that you might be thinking of. 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. Awesome by setting the value of one variable equal to the address of another that the variable now points to. to the other for new programmers, although most of the time that is not as easy as it seems, one of the most common problems that new programmers have with pointers is the syntax used to create them, the combination of stars, signs and arrows and more stars creates There is a lot of confusion so let's break this down using our previous example, in our last example we created an integer x whose value was 4 at hex location 1 000, after that we created a pointer that lived at hex address 1004 whose value was hexadecimal thousand, so how do we do that in C?
you will never ask about pointers again after watching this video
We could do it using two lines of code and I'll break it down part by part. The first line is int x equals four. This line is quite simple. The first integer part is the type of 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 name of the variable x and then we put an equal sign like when describing c we can verbalize that the equal sign is set to and then finally the value 4. So the final sentence we came up with is an integer whose name is x and is set to the value 4.
you will never ask about pointers again after watching this video
Well, the easy part next is the hard part to make a pointer to x like we did in our example. I would say that the next line of c int star px is equal to the x sign. 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 look at the dreaded asterisk, what does that mean? When an asterisk is placed next to a type, it modifies the type, which means that our variable is now a pointer to an integer, so our variable here points to a four-byte value next to the variable name , which is px or a pointer to , whe

never

you see an ampersand just think in your head the address of then this means the address of x our final sentence here is the int pointer px is set to the address of x so what does this do for us now when using the pointer Which do we have a way to access? x by reference instead of by value, so for example if we wanted to copy the value of , now what?
Is this code working? Let's break it down

again

why just like x is a normal integer, so there are no pointers yet, we say that y is set to use that equals sign. uh-oh, the asterisk again, remember how last time I said when we see an asterisk it is used to modify a type if a type is close to it, here there is no type when it is used just this way, the asterisk is known as reference d, reference d means to go to the address pointed by the pointer and take that value so that because px points to x, the reference d will go and take that value and set y equal to x, so when we verbalize c , when you see an asterisk by itself, you can tell that what is pointed out by this would mean that the final utterance of this line of c is an integer and is set to the object pointed to by p x by doing this we can pass x as a reference instead of value and why that matters.
I will explain in the next part of the video the final concept that What confuses people the most when learning about pointers is why someone uses them. The syntax is confusing. My programs crash all the time when I use them. Why does anyone bother? The answer is because we have to avoid creating code that is unreadable or unreadable. not scalable, we break down functions by the action they perform, so here I have a little c snippet where I have a function that updates the age structure of a person. The problem is that the struct I am editing is not within the scope of the edit function to fix this we pass the struct by reference so now the pointer to the struct is within the scope of the update struct and so Therefore, it can be edited using pointers like this, it keeps our code clean and understandable while reducing the amount of space we use.
When not copying, another reason pointers are inevitable when coding in C is the idea of ​​using static versus dynamic memory allocation. Static allocation is usually a variable that goes on the stack, a place that is always within the scope of the function that executes it, however when you are using dynamic allocations that come from the heap via malik or s-break or others types of memory allocators, you will get a pointer to memory that is out of scope if you ever want to be able to use this type of memory. You need to know how pointers work, the main difference between dynamic and static allocations is that static allocations are things that are known to have a fixed size at compile time, while dynamic allocations can be resized as you want the program is executed.
Here you will see that I assign a string. of 100 bytes to be popped from the heap, but those 100 bytes could have come from user input or something else if you're having difficulty with c, don't be discouraged, it takes a minute to master the tips, but once you get them you'll know and you will feel like a real low level magician when you do it guys I had a lot of fun making this video if you enjoyed it do me a favor like subscribe and see you next week take care.

If you have any copyright issue, please Contact