what are Pointers in C++ | Top latest 2021

Pointers in C++

Pointers in C++ slated to function pointers in the latest Article along with things like lambdas which I’m going to miss functions kind of without the full definition and all that stuff will get into that as we go along with us I want to start off with kind of the most primitive thing which is kind of what I like to call a role function point of which again comes from state so what is a function pointer so we learn about point is if you guys haven’t seen that Article it’s linked up that make sure understand what point is are you necessarily need to understand how memory works saw how point is working C. C++ to kind of use function pointers but you definitely do need to watch that Article so definitely check it out but basically function pointers I just a way to assign a function to a variable sorry functions usually as we’ve been using them now yeah I want to funk says by the way I could make an Article on that while back so definitely check that out as well but the weather was going to be using functions so far has just been like as something that we call right it’s not it we can’t really like do any sort of logic with functions. Pointers in C++ Pointers in C++

It’s just like a symbol that we can call when we want something to happen and we can of course give parameters and in 10 receive something in return if we write a function that returns something up invite Sir that’s how we’ve been using function so far however you can actually assign functions to variables and like to extend from that you can also possible intrinsic into other functions as parameters and there’s a whole bunch of things that you can actually do with functions that really crass to some interesting and complex logic that would otherwise be extremely messy to do it really cleans all that stuff up sorry the best way to demonstrate what a function pointer is and how to use and what I can do is. Obviously by example so let’s take a look Sir the first thing I want to do is write a function and this is going to be really simple it’s going to be avoid function it’s going to be cold hello wild and just kind of print hello welcome to start off real simple here okay Sir hello well there we go and then what I’m going to do is of course just because normally right that’s how functions work we call it this one doesn’t have any parameters so we need to put any brown is in and if I run my program of course. Pointers in C++ Pointers in C++

I’m going to get the tax hello world printing and you can see here that I do okay great perfect now let’s assign this function to some kind of variable now we learn about the order key what and the order Q. what is one of those things that is actually really useful with things like function pointers and will definitely add that in the future as well but let’s just give auto shop and see if we can assign hello world your order someone quite a variable called function I’m a cynical to color wild now that’s right away you can see this isn’t working because we con did you sort of tight because really hello world returns void and what we’re doing here is we’re actually holding the function but if we get rid of the parentheses right suddenly Pointers in C++ Pointers in C++ went on actually calling the function we’re actually getting the function point to and specifically this would look like this with an accent so what kind of getting the memory address all that function now functions are of course just a CPU instructions and there’s still some way in how binary when we actually compile a card in the future we might actually take a deep dive into the binary file and see our acts of the few instructions and. Pointers in C++ Pointers in C++

How that works but for now just imagine that literally when you compile your card every single function gets compiled into CPU instructions and ask someone that you know binary you know executable file right so what we’re doing over here with Santa stands with saying Hey in this executable file let’s find this hello world function and let’s get the memory address all of those CPU instruction that’s basically what this is right so what we’re doing is we’re retrieving the location all the instructions to execute when this function gets called and so now. We don’t actually have to use them to stand by the way because there is an implicit conversion with full stamina you can just do this right hello world and that’s it so now what I can do is I can actually call my function like this and that’s actually called twice Pointers in C++ Pointers in C++ I’ll hit F. 5 and you can see that I get hello well printed twice that’s pretty cool we’ve actually managed to assign a function to a variable now this is order but what type is this actually well if we ha also with this look at what we get we get void and then like an asterisk, ND function and some friends he looks a bit weird so it’s actually right at that time so we get invoice then we need to give us some kind of name an outcast because property of arable so before we call to function let’s call function again so in parentheses. Pointers in C++ Pointers in C++

I’m putting an asterisk followed by the name of my variable and then any parameters that my function my texts of the took it into a job I would put an end like that if it if the string open string now we’re not taking anything as you can say this is Justin are not crowded here so I can just kind of and like that this is the actual time now this does look weird because this well this is ultimately the type kind of fright it’s just that we need to give it a name like any other variable and function just happens to be the name Justin B. Chennai and this can be anything you want it’s just the name and then what we can kind of do here is just a sign John R. to say hello world or something like that because Pointers in C++ Pointers in C++ it was like any other variable and you can of course do that here as well right hello world. That also works like that okay it does look very confusing that’s going to be step one like I’ve called a charm of culture yeah like as if it was a function if I have 5 of course is still lacks this doesn’t look confusing I totally agree which is why it what people tend to do with function points is like this is either use all her all my kind of perfect way is actually to type definite using it so we’re basically putting an alias for so what I mean by that is instead of this kind of quick card well we can actually do is say type death then this actual time kea.  Pointers in C++ Pointers in C++

And we can call it like our honor hello world function or something like that right and of course this color will function name goes into here because that’s our actual name so it’s basically the time but with respect to how the front what that now means is that if I want to actually use this I can just type in hello world function then a variable name like function it calls hello world and then of course I can just call function like that. Okay let’s make this a little bit more exciting I’m going to add a parameter here is going to be in it’s just going to be cold Hey and then at the end of hello world I’m actually going to prince value and then Hey just like that well let’s put this on the right side all right so now this hello well function of course we get an error here we come to sign it because hello world is a function that takes in an integer so what I’m going to do is change my function definition here to actually include an integer as a parameter and I can see this wax and when we call this function which we’re doing every here we now need to specify its job someone’s talking ace and then hit F. 5 to run my card and you can see. Pointers in C++ Pointers in C++

Now I get the value 8 printing so what I’ve kind of done now is I have a function that I can coal using a variable name like this and of course is parameterized so I can put anything I wanted to hear and it will call that function with different values which again is pretty cool so that’s the that’s the basis of how it works that should probably show you enough to use it in pretty much all of its capacity but now let’s take a look at an actually useful example of why you might want to use function pointers in the first place the person that I have a back to just include vector here until late this card as well and all of this in fact I haven’t backed up all integers. Our goal is values find using this kind of initialize a list yeah. Like that okay and I want to create some kind of function within right through all of this and perform some kind of action sorry of course the for each loop is basically what I’m writing here but let’s pretend that I wanted to be in a function because maybe did some other things as well and that’s just the example so what I’m going to do is I’m going to write for each function of pulled up for H. it’s going to take in this kind of act up of integers.  Pointers in C++ Pointers in C++

I’ll use and then it’s going to do something so I’ll just say for instance the value in the values and we want to actually do something sorry let’s go ahead and call this. For each. Right now how do we tell this function what we wanted to actually do for each of these values well the way we’re going to do it is to just by passing in a function essentially so up here I want to create a function I’m going to call this printed value it’s going to take you know value and almost going to do is actually princess I value. And then the value. It’s very simple function right and so what I’m going to do here is inside for each I’m actually going to write 1 more promise yeah that actually is this function so it’s going to be void the name of article is funk and then of course it takes 1 parameter which is an anti-just somewhere right in like that and then insiders for each loop I’m actually just trying to write songs and then call it with value as the primary schools because that’s the current iteration fully and then inside us for H. Pointers in C++ Pointers in C++

I’m passing in the values of course which is that actor and I’m also going to Boston this function which is called print value so now basically what we’ve got is a function into which we can positive facto into jazz and for each of these integers inside the spectra it’s going to execute this function that’s what’s happening over here and you can see what that looks like so if I hit F. 5 you can see what I get here I get all these interest I’ve bought 1542 and 3 as I defined my list here and I’m getting this print value function executed with each of these and just as a parameter so that’s pretty cool it’s when we can kind of tell our function Hey I want you to do this at a particular time now this might seem a little bit early kill socially 1 more way to kind of do this and this is kind of late into the next Article that we have so it’s kind of funny as function it’s a bit messages have an extra phone for floating around especially if we only intend for it to be used inside this for each function so what we can do is use something called a lambda and lambda is essentially a normal function except it’s not declared as normal function like this is kind of the quiet during our part in this kind of a throwaway function that isn’t actually a real function as an anonymous function and the way that we can kind of the clan that is by using square brackets like this France’s like this.  Pointers in C++ Pointers in C++

We’re taking our actual ground that we need to take in here somehow you and then with Kelly brackets will basically tell us what we want to do sorry as if it was any other functions so we’ll save grand value. And then the actual value that we house which is going to be value which is the prominent with Austin okay just like that pretty simple stuff let’s get out 5 and you can save it I get my values printing like before but we don’t have an extra function with which we just kind of written and in line the square brackets I was cold caption methods that’s how we possible in from the outside world out parameters going kea and insight and this is just like any other function body we could even run it like this if you want to and you can see that’s the actual function okay we’re going to talk way more about Lyme disease in in an Article dedicated to Landis we’re also going to talk about something called STD function with a standard functions standard binds and all that stuff so there’s a lot more to actually discuss to do with function pointers this is just kind of a gentle introduction so that you can see what they are and how we can use them and all of that and also the primitive way of how they exist in the state which is just kind of like this void and then it’s just the signature is really weird and almost someone uses this an actual C. C++ anymore but will definitely be aware of it because you will see a lot of card like that just in your programming life anyway have you guys injured speaker if you did you hit that like button you can help support this year is not going to pay for the home full size to chatter there’s some really cool rewards. Pointers in C++ Pointers in C++

Also See

What are Functions in C++ | Easy and epic explanation 2021

Leave a Comment

Your email address will not be published. Required fields are marked *