C++ POINTERS (2020) - How to use pointers and arrays (for beginners) PROGRAMMING TUTORIAL

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] hi everyone welcome to my channel in this video i want to talk about pointers and arrays and if you watched the first video of this playlist you remember that i said that one of the common uses of pointers is using them with arrays so in this video i want to demonstrate how that works so let's jump straight to our visual studio and let's create an array of type and and i'm going to call that array lucky numbers like this and let's say that i have five lucky numbers for example and those are two three five seven nine oh that's five numbers already okay so these here are my lucky numbers and now i want to show you one thing so what is going to happen if i say for example c out lucky numbers and let's add a line so what is going to be written out if i write out just the name of my array let's check that if i run this program as you can see we get an address but what is this address this address here so the name of our array is actually the address of the first element of that array and in order to prove that let's write out the address of the first element of this array to check if these two are going to be the same so here i want to write out the address of the first element which is the element with index 0. so now if i'm telling the truth these two should be the same so if i run my program as you can see indeed we get the same address which means again the name of the array is the address of the first element of that array now that means that this lucky number's name behaves as a pointer and these square brackets here are behaving as a operator for dereferencing so if i say for example c out lucky numbers of two i'm going to get the element that has index two so this here is going to be the first address and then it is going to add two more addresses to that element and we are going to get the value that is stored there so lucky numbers name is the address of the first element it is going to add two more addresses to that so one two so this line of code here is going to give us the value of five let's add end line and run our program in order to demonstrate that and as you can see indeed we get the value of 5. now there is another way to do this same thing here and that is going to be the following way so i can say c out please write out this lucky numbers which as we already demonstrated is the address of this first element so i'm going to put that here and then i can use arithmetic operators on this so i can say please add to this lucky numbers two more address spaces and then dereference this so we use star symbol for that and then i'm going to add a line and these two lines of code so this one here and this one here should behave the same so if i run my program now as you can see we get the same value so these two lines of code are doing the same thing which means that this line of code here is using these square brackets in order to dereferentiate the element with that index which is index 2 and we know that the indexing starts with 0 so 0 1 2 and we get the value of 5 and then this second line of code here uses this star symbol in order to referentiate this expression here and what this expression here says it says lucky numbers which is the address of the first element as we demonstrated here so please use the address of the first element and then add two more address spaces to that so the address of the first element if i add two more to that it's going to be the address of one two so the address of this one here and then if i dereferentiate that i get this value which is stored on that address so these two lines of code are writing out the same thing so let me very quickly show you how you can enter the values for this array and then write them out because here we have hard-coded values so i'm going to delete this and um i'm going to comment these two lines of code as well no actually i'm going to comment all of these because we don't need them anymore but i'm going to leave them for you so that you can um see how these are used in order to dereferentiate um your array so as i said we want to enter values for our array now so in order to do that i'm going to use for loop so i will say four and then our for loop starts with i equal to 0 because that is the index of our first element and then considering that our array has 5 elements and the last one has index of four we are going to run our for loop while i is less than or equal to four and in each iteration we are going to increment the value of our i okay and in order to enter values for our array let's write out a message so i'm going to say see out number so please enter a number that is going to be the message for our user and then i'm going to input a value from my console into lucky numbers of i so this here means please enter the value into our lucky numbers array to element that has the same index that our i holds in that iteration so in the first iteration index 0 in the second iteration index 1 and then in the third iteration index 2 and so on so this for loop here is going to help us to enter the values into our lucky numbers array and in order to show that i'm going to put here a breakpoint and that means that our program is going to stop its execution when it comes to this line here so if i run my program as you can see it asks me to enter a number so i'm going to say 2 5 7 9 and 13. so those were five numbers that we had to enter for our lucky numbers array and if i press enter one more time as you can see our program has been stopped in this line of code here and if i hover over my lucky numbers here we have values that we have entered for our array so value of 2 5 7 9 and 13 which are the values that we have entered here so i'm going to stop my program now please stop okay and now what i want to do is i want to use this other approach to write out the numbers that this lucky numbers array is holding so let's use another for loop i'm going to copy this one okay and then what this second for loop is going to do it is just going to write out these numbers but okay let's use this approach for now so i'm going to just change this to c out and then use these other redirection signs and after each number i'm going to add an empty space okay like this and instead of using this approach here so instead of using these square brackets in order to differentiate the element on that position let's use this approach here so i'm going to say please use lucky numbers so which is the address of the first element and then to that i want to add the value of my i so plus i like this and this is going to move okay so this expression here is going to help us to access all of the elements of this lucky numbers array so from the one that has index 0 until we come to the one that has index four and because we don't want to write out addresses because this here is going to give us the address but we want to write out the values that are stored on those addresses we will have to de-reference this expression here so for that i will use star symbol and i'm going to remove this breakpoint now and i'm going to start my program to see what is going to happen okay and let's enter 2 3 5 7 19 and as you can see we get values of 2 3 5 7 and 19 which we have entered okay let's stop this program now and there is one more thing that i want to show you what is going to happen if i change this max value from 4 to 5 like this so what is going to happen in this particular situation let's run our program and enter numbers again so 2 5 7 13 15. okay as you can see now here we have these five numbers that we have entered so 2 5 7 13 15 those are the ones here and then the last one is something that we do not recognize what is this this is some junk number that we definitely have not entered here so this number here is something that does not belong to our lucky numbers array and here we have accessed someone else's memory space so we have this result because we change the max value from 4 to 5 here and as we already know our lucky numbers has five elements the last one having the index of four which is this one here and then here we added one more iteration so now we are trying to access the element with index five and that element with index five does not belong or does not exist in our lucky numbers array so this element here this is memory location that belongs to someone else and we accessed that trying to dereferentiate the pointer to the element that is sixth element so we have five elements in our lucky numbers array and then if we add one more address space to that we get this here which is memory location that belongs to someone else and if we just randomly change and access memory locations that do not belong to us we can cause very very big problems so i advise you to be very careful when iterating through your arrays and to access only memory locations that belong to that array that you are iterating so indicate that your array has five elements that those are going to be from zero to four so indexes from zero to four in the case that your array has ten elements for example those are going to be indexes zero to nine and so on so again be very very careful to not access memory locations that do not belong to you so i hope that now you have an idea on how pointers and arrays go together and as well we have seen some of the potential problems that can happen if you are not careful enough when working with pointers and arrays so again i advise you to be very careful uh when working with pointers and arrays and not access memory locations memory addresses that do not belong to you so thank you for watching this video if you enjoyed it if you learned something new give it a thumbs up and subscribe to my channel hit that bell icon as well you can follow me on my other social media if you want links for those are going to be in the description of this video thank you for watching and i'm going to see you in my next video bye
Info
Channel: CodeBeauty
Views: 37,184
Rating: 4.9499655 out of 5
Keywords: codebeauty, code beauty, c++, pointers, what are pointers used for, introduction to pointers, pointers tutorial, for beginners, pointers in c++, how to use pointers, easy, pointers in c, programming, course, tutorial, understand pointers, everything you need to know about pointers, learn pointers fast, arrays, pointers and arrays, relationship between pointers and arrays, how to use pointers and arrays, fast, name of the array
Id: DsS-ZiYOWy4
Channel Id: undefined
Length: 13min 2sec (782 seconds)
Published: Wed Oct 07 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.