Learn C++ With Me #15 - Pointers (*)
Skills:
LLM Foundations60%
Key Takeaways
The video covers the basics of pointers in C++, including how to create and use pointers, dereference pointers, and perform pointer arithmetic. The video also discusses the difference between pointers and references, and how pointers are used to access memory address locations. AlgoExpert is mentioned as a coding interview prep platform.
Full Transcript
hello everybody and welcome to another c plus plus tutorial for beginners in this video i'm going to be covering pointers now in the previous video i covered references please make sure you watch that before moving on to this video or you at least know what references are because that knowledge is going to be important to understand what i explain here so with that said let's talk about pointers [Music] all right so i've actually brought out the drawing tablet here to start explaining pointers kind of the blackboard whiteboard whatever you want to call it and anyways i think this will be helpful because i can do some illustrations and it will make it more clear you guys can actually see you know a visual of what's going on when we create a reference or we create a pointer with that said the what is a pointer now a pointer is actually a variable that stores the memory address location of some other value or object so a reference does not do that what a reference does is point to kind of the same box that another variable already points to so i'm going to do some drawings here to show you what i mean so if i create a variable let's say we make and x and we make that equal to 2. what kind of happens here is we have this box that gets drawn in our computer's memory we can label this the memory excuse my very messy handwriting here and anyways in the memory what happens is we have this box this box has a 2 inside of it this has some memory address location let's say 0x maybe 2 3 a b whatever and then we have some label to this right which is called x so x allows us to access this box right here now though when we create a reference so we say something like int and then we do my really bad ampersand y is equal to and then we make this equal to x what happens is y just points to this same box and so now whenever we create or sorry whenever we change the value of x or y really just changing whatever's in this box both of these values or both of these variables are going to change because they're both pointing to the same thing right that is what a reference does now this is different from a pointer i'm going to show you how we create a pointer so when we make a pointer what we can do is say something like int we can do maybe a zed and then before the z actually we write an asterisk so i'm going to say int and then asterisk z and then this needs to be equal to the memory address location of whatever we want this pointer to hold now understand this a little bit confusing here but the ampersand is the symbol or sorry not the ampersand but the asterisk is the symbol for pointer so whenever you initialize a pointer you use an ampersand i will show you on the in the code after obviously this is just a rough illustration but what i'm saying is i'm making a pointer called zed and i want this to store what well what it needs to store is a memory address location and so what i need to do is use the ampersand to access the memory address location of whatever this pointer wants to hold and in this case i will say x and so now what actually happens here is i've said int asterisk zed is equal to ampersand x and now instead of having z point to this box a new box is created and this box has its own location like zero x a b c d whatever and then inside of this box stores the memory location of whatever this is so 0x 2 3 a b and notice that 0x23ab is the address of the box that's storing the value 3. and so now what happens is z has this label kind of here that's going to this box and now if i access zed i get the memory address location of the variable x otherwise known as the variable y as well because that's an alias and so if i want to change the value in x i need to use this memory address location to modify this and you can do that i'll show how that works but notice the difference here we're actually storing a memory address location and if i were to actually look at the memory address location so i said ampersand zed what this would give me is this this other memory address location that stores the memory address location of the other variable so hopefully this kind of makes sense but that is how a pointer works and you can have nested pointers you have a pointer going to a pointer going to a pointer going to a pointer and well all of those are going to have their own memory address locations and they're all going to point to a different memory address location and this is why it's called a pointer is because we're given some box here that tells us how to point to a different location in memory and using pointers we can do all kinds of fancy stuff we can do points or arithmetic where we add one to a point we subtract one from a pointer like there's all kinds of wild stuff you can do with pointers and they're really useful but just really understand this difference at the pointer is its own value and that value tells us the location of a different separate value and so what we actually need to do as i said if we want to modify the value that this pointer is pointing to is we need to do what's known as dereferencing it now when you dereference a pointer what that does is give us the value that's actually stored in the memory address location that this pointer is holding and the way you dereference a pointer is you put an asterisk before it so if i'm accessing zed i would put an asterisk before it so that rather than getting this memory address location when i look at it it would give me the value stored in this memory address location so anyways i think that's enough for the illustrations here i'm going to pause this now i'll show you some examples in the code so we will continue in one second but i need to quickly thank the sponsor of this video and this series which is algo expert as you guys know i work at algo expert i am a proud employee there and proud instructor on the platform they have in my opinion genuinely the best platform to use to prepare for software engineering coding interviews everything from the curated questions to just a great environment to actually write code in just makes it a very pleasant experience when you're actually studying for your interviews with that said check out algo expert from the link in the description and use the code tech with tim for a discount on the platform all right so let's create some pointers here what i'm going to do to start is make a variable i'll say index is equal to 2 we'll go with our classic example here and i'll say int and i'll say asterisk y is equal to and then x now let's just start out by c outing both of these values so c out x and then end l and then c out y and then end l okay so let's run this and we get an issue oh the reason we got a problem is actually a good problem to run into i'll discuss this is because i said int and then pointer y is equal to x i did not put the memory address location of x so this is invalid because x is giving me 2 this is not a memory address location and so instead i need to put the ampersand to get the address of x so now when i do this it actually gives me the memory address of x right so just to show you this if i print out the memory address of x and then i print out y which is the pointer to x you'll see that both of these are the same thing however if i point out or print out sorry the memory address of y you're going to notice this is different than x right because this is a box that is storing the memory address of x so hopefully that's clear but that's like a really good illustration of the difference between a pointer and a reference right and so again this pointer is storing the actual memory address when i just access that pointer blindly right i just use y it gives me whatever it's storing which is the memory address of x however if i look at the memory address of the pointer that's different than the address that is holding and now to show you the dereference operator if i want to see the value associated with the memory address that this pointer is pointing to then i put an asterisk before it now you're going to see that we get 2. the reason we get 2 is kind of what happened is the computer went to whatever was being stored by y it saw that that was the location 2x and then looked up that location found the value in that location and returned it to us so this is the d reference operator so that is kind of the basics of pointers hopefully that gave you a good understanding of how they work and what they are okay so just a few notes here that relate to pointers that don't relate to references when you create a pointer you do not need to initialize it it does not need to be equal to something it is totally valid to have an empty pointer or like a default point or whatever you want to call it so in this case i initialized y no problem or sorry not initialized declared y but didn't initialize it wasn't a problem you can have a pointer that is equal to null again we haven't really covered that but this is still valid we won't get a problem if we do that we have a pointer that points to a different pointer that is totally valid as well we can do pointer arithmetic so we can do things like add pointers subtract pointers delete pointers won't really get into that too much uh but that's just like that differs from references right because with references you needed to initialize them they needed to actually reference a value or reference a variable they couldn't be equal to no and i didn't mention this in the previous video but you cannot change what a reference references whereas you can actually re-declare or redefine a pointer and have it point to something else so i can say something like in asterix y is equal to and then ampersand x and let's make another value here and w is equal to 3. i can change y by just saying y is equal to then ampersand w that is totally valid whereas if we were using a reference this would not be valid we could not do something like y equals ampersand w if y was a reference okay so now what i want to show you is something interesting which is using pointers with arrays so i'm going to create an array i'm going to say int x i think this is the syntax i always completely forget the array syntax it's all different in these different programming languages and we'll just make this like one two three okay so now what i want to do is actually get the pointer to the first uh location in this array so to do this i'm going to say int and i'll call this head is equal to ampersand x okay so now let me just see out and this sorry should be asterix head because this is a pointer let me see out head and see what we get and see if i made any mistakes so when i run this we get a problem can't convert int asterisk 3 to end asterisks in initialization okay let me have a look here and i will be right back okay so i realized i made a small mistake here i put the ampersand x now that was actually incorrect when we're talking about an array i understand this is confusing like sometimes you use it sometimes you don't need it anyways when we're talking about array i think i mentioned this in the previous video the array itself is actually just equal to the memory address of the first location in the array so when i say something like asterisk head is equal to x this actually gives me the location of the first element in the array automatically and so there's no need for me to put the ampersand x like that however i could do something like ampersand x0 if i do this we'll see that does actually work there's not a problem with that but by just doing ampersand x we get an issue and notice this is what ampersand x0 is now let's look at what we get it was ending in eb0 okay let's go again when we just look at x same thing eb0 right okay so now what i want to show you is how we can loop through this array just using its pointers and pointer arithmetic so what i'm going to do is i'm going to say 4 we can say something like int i is equal to 0 we can say i is less than and i'll just hard code in 3 here i won't do like the size of array or anything and then i'll say plus plus i although we could do i plus plus as well and now what i'm going to do is change head on every iteration so i'm going to say head is equal to x plus i and then what i'm going to do is say c out and i'm going to c out asterix head and l and actually first let's just see out head before we do asterisk's head and see what we get here so if i run this notice we get different memory locations we get 0x 61 fe ac and then b0 and then b4 okay we added i which was either 0 1 or 2 to x great so now what i'm going to do is i'm going to see out asterix head and see what we get and when i do this we get 1 2 and 3 the values of our array now let's change this to be 4 5 6. let's run this notice we still get 4 5 6. so the point of me showing you this is that if we add some value to a pointer that simply increments the address of that pointer and in this instance here what we just did is access the first address second address and third address of the elements stored in this array because we had the first address we knew that all of the values after were stored in the subsequent addresses right so in like the plus one the plus two of the address to make this really simple imagine the head is equal to 1 right so element 4 is equal to 1 then element 5 would be equal to 2 and element 6 would be equal to 3. so by adding 1 to the head each time that allowed us to access the subsequent elements and that's what i was trying to show you here with this pointer arithmetic that you can do something like add something to a pointer and then i dereference this pointer that i was changing every time to actually get the value associated with all of these elements so with that said i think this is a good point to leave it at hopefully this was a good introduction to pointers at least allowed you to understand the difference between a pointer and a reference this stuff comes with practice and these symbols are really confusing when you see the asterisks to define a pointer and then you use it to dereference then you use the ampersand to define a reference then you use it to get the memory address location like they don't make this very easy i don't know why they couldn't pick some other temp symbols to use but regardless this is the way it is this is a pointer and this is a reference so in future videos we'll see more of this and we'll see kind of why we actually would want to use these things but for now just understanding these topics is really all we need to do with that said if you guys like this video make sure you leave a like subscribe to the channel and i will see you in another one
Original Description
Welcome back to another C++ tutorial! In this video, we will be covering pointers. In the last video, we went over references, it's important that you watch the previous video or have an understanding of references before watching this one.
💻 AlgoExpert is the coding interview prep platform that I used to ace my Microsoft and Shopify interviews. Check it out and get a discount on the platform using the code "techwithtim" https://algoexpert.io/techwithtim
🔍 Playlist: https://youtube.com/playlist?list=PLzMcBGfZo4-lmGC8VW0iu6qfMHjy7gLQ3
⭐️ Timestamps ⭐️
00:00 | Introduction
00:29 | Pointers and References Visualized
06:09 | Pointer Examples
08:21 | Pointer Rules
09:47 | Pointer Arithmetic With Arrays
◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️
💰 Courses & Merch 💰
💻 The Fundamentals of Programming w/ Python: https://tech-with-tim.teachable.com/p/the-fundamentals-of-programming-with-python
👕 Merchandise: https://teespring.com/stores/tech-with-tim-merch-shop
🔗 Social Medias 🔗
📸 Instagram: https://www.instagram.com/tech_with_tim
📱 Twitter: https://twitter.com/TechWithTimm
⭐ Discord: https://discord.gg/twt
📝 LinkedIn: https://www.linkedin.com/in/tim-ruscica-82631b179/
🌎 Website: https://techwithtim.net
📂 GitHub: https://github.com/techwithtim
🔊 Podcast: https://anchor.fm/tech-with-tim
🎬 My YouTube Gear 🎬
🎥 Main Camera (EOS Canon 90D): https://amzn.to/3cY23y9
🎥 Secondary Camera (Panasonic Lumix G7): https://amzn.to/3fl2iEV
📹 Main Lens (EFS 24mm f/2.8): https://amzn.to/2Yuol5r
🕹 Tripod: https://amzn.to/3hpSprv
🎤 Main Microphone (Rode NT1): https://amzn.to/2HrZxXc
🎤 Secondary Microphone (Synco Wireless Lapel System): https://amzn.to/3e07Swl
🎤 Third Microphone (Rode NTG4+): https://amzn.to/3oi0v8Z
☀️ Lights: https://amzn.to/2ApeiXr
⌨ Keyboard (Daskeyboard 4Q): https://amzn.to/2YpN5vm
🖱 Mouse (Logitech MX Master): https://amzn.to/2HsmRDN
📸 Webcam (Logitech 1080p Pro): https://amzn.to/2B2IXcQ
📢 Speaker (Beats Pill): https://amzn.to/2XYc5ef
🎧
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from Tech With Tim · Tech With Tim · 0 of 60
← Previous
Next →
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
A* Path Finding Algorithm(Visualization)
Tech With Tim
Python Programming Tutorial #1 - Variables and Data Types
Tech With Tim
Python Programming Tutorial #2 - Basic Operators and Input
Tech With Tim
Python Programming Tutorial #3 - Conditions
Tech With Tim
Python Programming Tutorial #4 - IF/ELIF/ELSE
Tech With Tim
Python Programming Tutorial #5 - Chained Conditionals and Nested Statements
Tech With Tim
Python Programming Tutorial #6 - For Loops
Tech With Tim
Python Programming Tutorial #7 - While Loops
Tech With Tim
Python Programming Tutorial #8 - Lists and Tuples
Tech With Tim
Python Programming Tutorial #9 - Iteration by Item (For Loops Continued...)
Tech With Tim
Python Programming Tutorial #10 - String Methods
Tech With Tim
How to Overclock a NVIDIA GPU
Tech With Tim
Python Programming Tutorial #11 - Slice Operator
Tech With Tim
Python Programming Tutorial #12 - Functions
Tech With Tim
Python Programming Tutorial #13 - How to Read a Text File
Tech With Tim
Python Programming Tutorial #14 - Writing to a Text File
Tech With Tim
Python Programming Tutorial #15 - Using .count() and .find()
Tech With Tim
Python Programming Tutorial #16 - Introduction to Modular Programming
Tech With Tim
Python Programming Tutorial #17 - Optional Parameters
Tech With Tim
Python Programming Tutorial #18 - Try and Except (Python Error Handling)
Tech With Tim
Python Programming Tutorial #19 - Global vs Local Variables
Tech With Tim
Python Programming Tutorial #20 - Classes and Objects
Tech With Tim
Cool VBS Script to Prank Your Friends!
Tech With Tim
How to Overclock an AMD GPU
Tech With Tim
Best GPU'S For Mining Ethereum (2018)
Tech With Tim
Recursion and Memoization Tutorial Python
Tech With Tim
Ethereum Mining Rig - Hardware Guide
Tech With Tim
Pygame Tutorial #1 - Basic Movement and Key Presses
Tech With Tim
How to Install Pygame (Windows 8/10)
Tech With Tim
How to Trade Your Cryptocurrency (Bitcoin, Ethereum etc.) For Cash!
Tech With Tim
How to Mine Ethereum 2018 - WORKING (Super-Easy)
Tech With Tim
Microphone Comparison - $10 Mic vs $150 Mic (Blue Yeti USB)
Tech With Tim
Pygame Tutorial #2 - Jumping and Boundaries
Tech With Tim
Pygame Tutorial #3 - Character Animation & Sprites
Tech With Tim
Pygame Tutorial #4 - Optimization & OOP
Tech With Tim
OBS Studio Tutorial - Best OBS Settings
Tech With Tim
Linear Search Algorithm - Python Example and Code
Tech With Tim
Make Any Mic Sound AMAZING! (WITH OBS)
Tech With Tim
Binary Search Algorithm - Python Example & Code
Tech With Tim
Pygame Tutorial #5 - Projectiles
Tech With Tim
Pygame Game - Mini Golf
Tech With Tim
Pygame Tutorial - Projectile Motion (Part 1)
Tech With Tim
Pygame Tutorial - Projectile Motion (Part 2)
Tech With Tim
Pygame Tutorial #6 - Enemies
Tech With Tim
Pygame Tutorial #7 - Collision and Hit Boxes
Tech With Tim
Pygame Tutorial #8 - Scoring and Health Bars
Tech With Tim
Cloud Mining vs. Hardware Mining - 2018
Tech With Tim
How to Install Pygame on Mac OSX (Fast-Simple)
Tech With Tim
Pygame Tutorial #9 - Sound Effects, Music & More Collision
Tech With Tim
Pygame Tutorial #10 - Finishing Touches & Next Steps
Tech With Tim
How to Fade Your Screen in Pygame [CODE IN DESCRIPTION]
Tech With Tim
How to Create a Button in Pygame [CODE IN DESCRIPTION]
Tech With Tim
Pygame Side-Scroller Tutorial #1 - Scrolling Background/Character Movement
Tech With Tim
Pygame Side-Scroller Tutorial #2 - Random Object Generation
Tech With Tim
Pygame Side-Scroller Tutorial #3 - Collision
Tech With Tim
Pygame Side-Scroller Tutorial #4 - Scoring and End Screen
Tech With Tim
How to Create A Message Box in Python - Tkinter
Tech With Tim
Is Ethereum Mining Still Profitable - Is It Worth It (April 2018)
Tech With Tim
How to Run MAC OSX on a WINDOWS PC (Clover Boot-loader)
Tech With Tim
Programming Problem #1 - Alphabet Soup (Beginner/Novice)
Tech With Tim
More on: LLM Foundations
View skill →Related AI Lessons
⚡
⚡
⚡
⚡
X now offers an MCP server to make its platform easier for AI tools to use
TechCrunch AI
n8n Automation Repurpose Video Content: The 2025 Production Guide
Dev.to AI
You’re Still Paying $200/Month for AI Tools You Could Replace With a Free Local Setup Tonight
Medium · Data Science
Top 10 AI Tools Every College Student Should Know in 2026
Medium · AI
Chapters (5)
| Introduction
0:29
| Pointers and References Visualized
6:09
| Pointer Examples
8:21
| Pointer Rules
9:47
| Pointer Arithmetic With Arrays
🎓
Tutor Explanation
DeepCamp AI