C Programming Tutorial 94 - Intro to Pointers and Indirection Operator

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey what is up everybody this video we're going to introduce pointers and in the next video we're going to get some hands-on practice with them they can be pretty darn confusing for beginners so I'm hoping I can give you guys some pointers alright enough of that nonsense just before we get started I just wanted to mention that I am producing AC programming crash course which is basically a really awesome version of the series hands-on condensed it's great check it out if you're looking to interview or get some more practice with C programming and you don't want to have to go and watch this whole 100 part series again thank you guys and uh just to check that out and let's get started so what the heck is a pointer so this kind of takes us to new grounds and programming especially if you basically if you've never programmed then this is even more crazy but even if you've used other programming languages they don't really use pointers so it's it's new and because of that it's it's challenging so what in the heck is a pointer it's kind of confusing for beginners and that's okay if you have a hard time but I'm going to make it very simple for you guys first think of a normal variable int X and this is assigned some value now when we do this inside of our computer there's an area called of memory there's an area of memory another name for memory is RAM so inside of our memory there is a spot holding the value of X so if you wanted to think about it that way just there's a cell and in our memory with the value 5 now a pointer is a way to point to this location of memory so we can actually basically change the value of this memory or reference it but not using the variable X we're referencing it indirectly through a pointer the way you create a pointer is you say what type you want to point to so we're going to point to an int and then you give it a name so what we're gonna say pound sign Pete you have to use the pound sign and that is to indicate that it's a pointer typically that's attached to the name of the variable here and then we can say is you can set it equal to a value just like we do this this is this is a an integer variable X well here this is an integer pointer P then what we do is we can use the address of operator which we talked about in earlier videos but that's going to get the address of a variable in memory X and what this is going to do is it's going to create a variable P and it's going to point to five so it's kind of like a variable equal to five but not quite it's the the address of the variable equal to five so that means if we change X to ten well that means we replaced this value of ten thus the the pointer now points to ten same thing we can actually change the value through the pointer so essentially now we have the original variable containing the value 10 and we made another variable to to point to the same data through a pointer pointing to the value 10 so it's kind of like having two variables and they can both be used to reference the same data one directly and one indirectly through a pointer so pointers can be a little bit confusing at first because it's kind of like one of those things where it's hard to see the benefit of them until you use them a bunch and it's kind of like like when you're a kid and you're starting to learn multiplication you're like what am I ever gonna do with this in life but as you're older you start to understand those things maybe that was a terrible example it's just something you have to practice and then you're like oh I see how this is beneficial but for now just learn the fundamentals and understand these concepts and I promise you it'll be valuable later so the next thing you need to know is what's known as the indirection operator and it looks like the asterisk so this is confusing this is what I don't like but we just have to deal with it when we declared the variable P we did it like so and this this asterisk here says this is a pointer this is different than the asterisk here which is the indirection operator I hate that but we just got to deal with it so when we declare the variable we we use an asterisk then if we want to get the value that that variable is pointing to so P points to the value 5 if we want to get that value we use the indirection operator before the the variable P so what that means since P was originally pointing to X what we could do is we could say x equals 10 and change that value to 10 or what we could do is we could say the the value at P is equal to 20 and that'll change the value at this point if you printed X it would print 20 so the benefit here is that once we understand pointers we can use pointers to functions and these this will allow functions to change the values of variables and that can be really valuable if you need to do something like a swap function you're not able to change the value of variables unless you're using pointers so definitely useful definitely important you just gotta wrap your brain around some different concepts the 3 things you really need to understand is how you declare them like this how you assign to them which is like this P equals address of some variable X and then so that's the second way that's the second thing you need to know and then how you reference the value where the pointer points to which is with the indirection operator so this can also be called dereferencing so if you dereference a pointer you're basically getting the value where that pointer is pointing to hopefully that's not too crazy I think it'll be more concrete once you watch the next video where we're going to be doing all this stuff in Co NC it's gonna be delicious so check that out thank you guys and please be sure to subscribe peace out [Music]
Info
Channel: Caleb Curry
Views: 36,471
Rating: 4.9549212 out of 5
Keywords: c programming, programming, tutorial, tutorial 94, intro, to, pointers, and, indirection, operator, code, coding, caleb, curry
Id: ePutOtexvw8
Channel Id: undefined
Length: 7min 14sec (434 seconds)
Published: Mon Aug 12 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.