What is the Difference Between a Pointer and a Reference C++

  Рет қаралды 427,008

Paul Programming

Paul Programming

7 жыл бұрын

In this video I explain the difference between a C++ pointer and a C++ reference.
Donate - bit.ly/17vCDFx
STILL NEED MORE HELP?
Connect one-on-one with a Programming Tutor. Click the link below:
trk.justanswer.com/aff_c?offe...
:)

Пікірлер: 472
@systemofapwne
@systemofapwne 2 жыл бұрын
In short: Pointers and references can be used to refer to data in memory, without copying the data to a new variable. That is efficient. While pointers can arbitrarily be changed to point to any memory addresses and need a dereferentiation in order to read the value, references do refer to a fixed location in memory and behave like normal variables (do not need dereferentiation), when one wants to read their value. However, since no data is copied from one var to another in either case, changes made to these memory locations will be present instantly for pointers and references.
@VampyrumFerox
@VampyrumFerox 2 жыл бұрын
Nice explanation, but next time could you drag that out into a 3 hour video? I get paid by the hour. Thanks.
@Singularitarian
@Singularitarian 2 жыл бұрын
What’s the reason for the language to have references at all? Why not just pointers? It doesn’t seem like references can do anything we couldn’t already do with pointers.
@AxeMask
@AxeMask 2 жыл бұрын
@@Singularitarian I'm relatively new to C++ and pointers / references so my answer could be wrong but I had the same question after watching the video initially. However, upon further thought, I don't think that references are used in the way that the video demonstrated. I think the main use case is to use a reference value as the formal parameter of a function so that when you're passing large values to a function, you aren't compromising performance by copying the entire value; hence the terms 'pass by value' vs. 'pass by reference'. That being said, I could also be completely wrong so if anyone sees this and can clarify for us, that would be greatly appreciated. Cheers!
@Odb718
@Odb718 2 жыл бұрын
@@Singularitarian I think sometimes you have to "decode" a variable and pointers may not have to go through that to work in an equation. It might make a cheap way to have a "global variable" by making a ref to it. It's probably a read only type situation. I'm not sure lol that's why I watched this video....
@anonym3852
@anonym3852 2 жыл бұрын
@@AxeMask Pointers are not in every programming language. Take PHP as an example, they only work with references. The key differences with an example: A pointer is a variable, that points to the address of another variable, but it CAN be changed, CAN be NULL AND you can dereference it, so you can access the memory itself or the address to it. A reference is a variable, that points to the address of another variable, but it CANT be changed and CANT be NULL. Lets say you have a function which always expect a User object. Than you dont want a pointer. A pointer could be NULL, so your function cant work and there will be errors if it is NULL. You could check before, if its not NULL, but this costs additional ressources. Also someone could change the value where the pointer is pointing too within their memory. This can be unexpected behavior. Better would be a reference in that case. You can be sure with references, that they have a valid value of the specific type (in this example a User object). Also for some styleguide if there is a &foo parameter in your function(if not const), you can assume it will probably get changed. Lets say you have an array with object. (Dont mind me if the syntax is not completly right, havent used C++ in a while, i try to make it at least somehow clear what i try to do and fk styleguides on yt) public function getTheHighestNumber(&numbers) { *highestNumber = numbers[0]; //Get first number of numbers array count = size(numbers); for(i = 1; i
@5r1zan
@5r1zan 7 жыл бұрын
Liked the idea of split screen.... helps visualising the code.... awesome concept....
@umarajmal6216
@umarajmal6216 4 жыл бұрын
@Sara Kiba 😂😂
@terrymoist
@terrymoist 3 жыл бұрын
I can't understate how easy and simple you made this to understand, very epic.
@Timo-Epis
@Timo-Epis Жыл бұрын
These nerds try to act like they have an IQ of 300 by explaining things in a difficult way.
@carlpeterkirkebo2036
@carlpeterkirkebo2036 9 ай бұрын
Totally agree!
@facts-nuk
@facts-nuk 3 жыл бұрын
It's been four year by now but still it is precious. This video make my day.
@chandrikasolankifashioncol1225
@chandrikasolankifashioncol1225 7 жыл бұрын
I read numbers of books but all confused. Well explained. Now it's clear. Thanks.
@ibrahimg.4469
@ibrahimg.4469 3 жыл бұрын
today good books = good tutorial video
@sammusaev5883
@sammusaev5883 3 жыл бұрын
@Greyson Francis whoa! go away
@HonsHon
@HonsHon 2 жыл бұрын
I read every book ever made but this video was better than all of them. Even Lord of the Rings
@moxieandreas9374
@moxieandreas9374 2 жыл бұрын
Just dont read books
@blackblocky3866
@blackblocky3866 Жыл бұрын
Literally the best explanation of the pointers in all of coding history.
@dilute6980
@dilute6980 2 жыл бұрын
1 small thing you didnt mention is that pointers also have their own memory address. Just like any variable calling operator & on a pointer returns the address of the pointer, which is different to the value of the pointer, being a memory address that it is pointing to
@alcurb
@alcurb 6 жыл бұрын
Great explanation. Simple and concise. I've been taking a Lynda course C++ Essential Training by Bill Weinman but he blows through the explanation leaving me in the dust. I could see the results of what was happening in his code example, but it lacked the graphical elements that would help me understand. You did that, and it is now clear. Thanks.
@Adam-gj3dn
@Adam-gj3dn 4 жыл бұрын
Definitely the best video on KZfaq explaining the difference between pointers and references. Also, thank you for not using all the programming jargon when explaining. Thank you!
@bestredditstories1158
@bestredditstories1158 2 жыл бұрын
I can't believe a seven-minute video summed up a concept that I did not understand for the longest time. Thank you so much for this.
@nicholashendrata
@nicholashendrata 4 жыл бұрын
THIS IS THE BEST VIDEO I'VE EVER SEEN FOR EXPLAINING POINTERS AND REFERENCES I CAN'T THANK YOU ENOUGH.
@lachlanvanderdrift7013
@lachlanvanderdrift7013 2 жыл бұрын
bro, you made this so easy to understand. you're such a genius, I cant wait to come back to this channel for more things like this when I need it. keep it up
@XxMonkeyACCxX
@XxMonkeyACCxX 2 жыл бұрын
i really appreciate writing the code while explaining the concept. I struggle understanding this concept with just drawings alone and then carrying it over to my projects. thank you
@hishamakmal2149
@hishamakmal2149 2 жыл бұрын
This is by far the best explanation on this topic. Thanks a million Paul!
@Florakinz
@Florakinz 5 жыл бұрын
Best video on the basics of pointers and reference. Period. :)
@FirezFlightz
@FirezFlightz 4 жыл бұрын
I know this is 3 years later but, this is the best most understandable pointer video ive ever seen ! Thank you for making it!
@Squash101
@Squash101 18 күн бұрын
Hands down the absolute best video on pointers I've even seen! I'm excited to watch your videos on LL and the rest of the different types of DS.
@Maxpain666777
@Maxpain666777 4 жыл бұрын
Absolutely amazing on the splitscreen teaching. Thank you so much.
@robinlee-delisle8591
@robinlee-delisle8591 6 жыл бұрын
Perfect explanation and the visualisation used really aided in my learning!
@xYuki91x
@xYuki91x 4 жыл бұрын
You explained it waaaay better than my professor. I finally understood it thanks to your video!
@anmol2757
@anmol2757 3 жыл бұрын
Man that's the best explanation I found online. Thanks a ton bro and kudos to your effort . All my doubts were cleared
@muallaozdemir1012
@muallaozdemir1012 2 жыл бұрын
I loved how you explained this, so easy and clear. Thank you.
@enigma2886
@enigma2886 7 жыл бұрын
You are a life saviour ....man thank you so much....thank you so so so much !!!!!
@brandicook8497
@brandicook8497 4 жыл бұрын
Thank you for simplifying this and for the visual representation. I actually understand it now !!!!
@Gidid56
@Gidid56 2 жыл бұрын
The diagram gives the impression that ptr holds a meaningful value after int* ptr; It does not. It holds garbage. It does not get assigned any value. It will just contain whatever garbage is in memory the moment ptr gets created.. Also references are under the hood pointers that behave like value types. But they are not value types. Once the original value object they are referring to goes out of scope, the refernce now is dangling (invalid). You have to make sure, that your original value object outlives the lifetime of the reference.
@crptc5707
@crptc5707 4 жыл бұрын
Much appreciated, it's clear and concise, saved us a lot and time and hassle!
@iVuDang
@iVuDang Жыл бұрын
love the visualization, conciseness, and easy to follow style of this video , thank you
@donyadabiri9906
@donyadabiri9906 4 жыл бұрын
Great video. It completely cleared the difference between pointer and reference for me. Also, using diagram helped so much in understanding the concept.
@jonathanp5195
@jonathanp5195 2 жыл бұрын
I'm amazed by how elegantly you explained this. Thank you for being such a great instructor!
@iyobosajefferson6457
@iyobosajefferson6457 Жыл бұрын
Love how this was concise and diagrammatically explained.
@Cagruntas
@Cagruntas 7 жыл бұрын
Damn been looking for a good video on pointers for a while
@Cagruntas
@Cagruntas 7 жыл бұрын
I found it
@FiicticioID
@FiicticioID Жыл бұрын
having c++ classes for 4 months, and i never had such a simple explanation between reference and pointers
@abhishekshah11
@abhishekshah11 4 жыл бұрын
Clear and concise. Just the way I love it.
@jenweatherwax7113
@jenweatherwax7113 4 жыл бұрын
This visual approach helped me a lot!!!
@OpheliaSHolmes
@OpheliaSHolmes 3 жыл бұрын
I enjoy the way you explained pointers, the chart helped a lot. Thanks!
@DonBeleren
@DonBeleren 4 жыл бұрын
Finally, someone explains this concept simply! :)
@MoodyDood
@MoodyDood 11 ай бұрын
I suffered with pointers and references for close to a year, thanks a ton!
@CarsonCameronClark
@CarsonCameronClark 4 жыл бұрын
Most flawless explination I've ever seen of any programming concept. Well done and thank you.
@ConorBrah
@ConorBrah 4 жыл бұрын
This was very clear and concise, thankyou!
@MukeshKumar-oc8or
@MukeshKumar-oc8or Жыл бұрын
Thank you for this tutorial! It was clear, concise, and easy to follow.
@arpeggio1231
@arpeggio1231 Жыл бұрын
bro so easy and simple explanation, I got a friend who has 35 years in programming and he didn't make it easy to understand. Thanks again brother
@tibyansuliman8657
@tibyansuliman8657 4 жыл бұрын
Paul your videos saves me every time 😭❤️ thanks a lot ❤️❤️❤️❤️❤️❤️
@nikolayrangelov1081
@nikolayrangelov1081 4 жыл бұрын
Hello, very good explanation! Can you cover in your next videos more advanced topics such as *&, **&. I know it is a combination of pointers and references, but extra explanation and exercises always help! Thank you!
@o2dyt
@o2dyt 6 жыл бұрын
My GF has a test tomorrow about this. You just helped me make it clearer for her. Thanks!
@RaceCondition.
@RaceCondition. 6 жыл бұрын
Love this video Paul! Keep it coming..
@sebastienboutin8588
@sebastienboutin8588 3 жыл бұрын
Hey nice video. It could have been nice to explore in more detail the difference using references vs pointers as arguments with functions, inside classes, with more complex objects, etc... But thanks for the video anyway!
@anastasijad4830
@anastasijad4830 2 жыл бұрын
beautifully explained.. i got goosebumps when i finally figured it out
@niquebon
@niquebon 2 жыл бұрын
Huge huge thanks to you! such a simple explanation to understand.
@blackcoffeedevelopment3522
@blackcoffeedevelopment3522 Жыл бұрын
short, simple, and helpful. Thanks!
@rpersen
@rpersen 2 жыл бұрын
Great explanation. Remember I struggled with this 20 years ago. Subbed.
@jhondoe3381
@jhondoe3381 3 жыл бұрын
Thank you for explaining it so well, I finally understood it.
@cauhxmilloy7670
@cauhxmilloy7670 2 жыл бұрын
Slight correction, pointers and references are really the same thing only differing slightly with syntax. You could liken a reference to a pointer that is never null and can only be set once (when declared). There are surely some syntax differences when working with pointers (no need to dereference, dot operator instead of arrow, etc), but they are really doing the same thing otherwise. You can verify by messing around with godbolt (looking at disassembly). Also note that you can do polymorphism with references. Thanks for the C++ content! The world needs more C++ enthusiasts! :)
@rajcodes100
@rajcodes100 4 жыл бұрын
Great lecture but what I find a little tricky is that initially when we introduced the ref we used & ref = var , now if we want the pointer to point to var we write ptr = &var and if we want the pointer to point to ref which is var we write ptr = &ref .In a mathematical sense people can get confused here because initially & ref = var and (ptr = &var or ptr = &ref but &ref = var so in this sense it becomes like var = &var).
@weiqianzhang981
@weiqianzhang981 Жыл бұрын
by far the clearest video I've found.
@pedroazevedo8868
@pedroazevedo8868 4 жыл бұрын
Man, thanks for this video, you are truly amazing!
@mykalesalad
@mykalesalad Жыл бұрын
Definitely less confusing now, thank you for the thorough explanation.
@ethannnnnnn
@ethannnnnnn 6 жыл бұрын
Honestly the best explanation of pointers I have ever seen. The diagram helped an unbelievable amount. I can't thank you enough.
@ChandraShekhar-by3cd
@ChandraShekhar-by3cd 6 жыл бұрын
Awesome Explanation!
@PunmasterSTP
@PunmasterSTP 2 жыл бұрын
This was incredibly useful. Thank you for sharing!
@auntikjeb6600
@auntikjeb6600 Жыл бұрын
You explained it so well i tried to understand the difference the whole day. Thank you🎉
@SHADERSOP
@SHADERSOP 2 жыл бұрын
What a fantastic explanation - great work!
@taki-eddinechih5197
@taki-eddinechih5197 6 ай бұрын
the best explanation I've ever seen about pointers
@HelicopterRidesForCommunists
@HelicopterRidesForCommunists 2 жыл бұрын
Visuals helped tremendously. Thank you!
@leam1978
@leam1978 2 жыл бұрын
before i switched my career to art, i was always confused about this. thanks for the explanation; might come in handy some day.
@zefarmer
@zefarmer 3 жыл бұрын
Clear and concise, thank you!
@muhammadbilal3942
@muhammadbilal3942 5 жыл бұрын
Sir ,it was great video ,cleared my concepts very well .
@Creuilcreuil
@Creuilcreuil 6 жыл бұрын
basically a ref is an immutable pointer, i.e *int* * *const* ptr; which automatically deferenced by the compiler when used, then nulled
@ratgr
@ratgr 4 жыл бұрын
never nulled
@thulanitinashe6475
@thulanitinashe6475 4 жыл бұрын
can you explain more cause i am lost
@jaimealbertorocha5961
@jaimealbertorocha5961 3 жыл бұрын
Pointers have their own intrinsic address (memory address of the variable or object in question), while references do not. The intrinsic direction of the pointer is different from that of the variable or object it points to. Instead, the intrinsic direction of the reference is the same as the variable or object to which it was associated. The reference is simply an alias for the variable or object to which it is associated. As a complement, you can think of a reference as another name that is given to the variable or object to which said reference is associated. That is, the variable or object whose memory address is associated with the reference will no longer have a way of being called but 2. In other words, the variable or object that is assigned to the reference can be called with two different names, either with the name of the reference or with the name of the variable or object that is assigned to the reference.
@alexdulhac6954
@alexdulhac6954 2 жыл бұрын
thanks for help man, short and sweet video
@BernhardWeber-l5b
@BernhardWeber-l5b 2 жыл бұрын
This is the most excellent explanation video one can imagine
@carboyau
@carboyau 7 жыл бұрын
This was great. I've struggled with C/C++ coming from a background in VB.
@dorbenmoyal7639
@dorbenmoyal7639 4 жыл бұрын
Exactly what Im looked for, Thanks !!!
@4AneR
@4AneR 4 жыл бұрын
I like the idea of thinking about a reference as an always dereferenced pointer with constant value
@sd9302
@sd9302 Жыл бұрын
Wow! Can't express my gratitude in words! Thanks sir
@rikvandenreijen
@rikvandenreijen 6 жыл бұрын
Hey awesome video, this helped me a lot. Thanks!
@nilupulperera
@nilupulperera 5 жыл бұрын
Nicely explained. Thank you very much.
@nicholasgodshall2511
@nicholasgodshall2511 2 жыл бұрын
Great use of visuals. Thank you.
@iiobl
@iiobl 6 жыл бұрын
Thank you for your video, very helpful!
@queiyanglim
@queiyanglim 7 жыл бұрын
Very clear explanation. Many thanks.
@sher.5027
@sher.5027 2 жыл бұрын
Thankyou. it was a great explanation compared to other youtube videos. Thanks again.
@Sub-zero1123
@Sub-zero1123 3 жыл бұрын
The best video that explains pointers. Thank you
@PaulProgramming
@PaulProgramming 3 жыл бұрын
Happy to help!
@dawidp4227
@dawidp4227 2 жыл бұрын
I'm just nitpicking here, but it's impossible to have two ints on addresses 0xA and 0xB, because int is usually 4 bytes, so the integer on address 0xA actually lives on 0xB, 0xC and 0xD too. I know it doesn't make a lotta difference, but I think it's good to know
@rocket2739
@rocket2739 2 жыл бұрын
A could be 0x66294701 and B could be 0x55399572 In other words, A and B are names, and B != A + 1 That's why he used uppercases instead of writing 0x0000000a and 0x0000000b
@francoisloriot2674
@francoisloriot2674 4 ай бұрын
it's also impossible to have 4 bits (single hex digit) memory address on any usable computer. So your knit picking missed the point that these just mean arbitrary addresses A and B.
@yannmasoch
@yannmasoch 6 жыл бұрын
Pointers and references in C++ are not easy to figure out, and your explanation makes things easier! Thanks :)
@melissevli1646
@melissevli1646 4 жыл бұрын
It was so clear , thank you !!
@baronvonmike
@baronvonmike Жыл бұрын
Finally, someone just explains what an "alias" is.
@rajnikantjaiswal4656
@rajnikantjaiswal4656 7 жыл бұрын
now i know exactly the difference b/w reference and pointer..Thanks paul...
@louisboscher33
@louisboscher33 2 жыл бұрын
Dude thank you so much, so useful, so well explained you are the best
@mariosasic4251
@mariosasic4251 4 жыл бұрын
Thank you, very well explained!
@lmthelex
@lmthelex Жыл бұрын
te amo loco, viva el conocimiento libre y a disposicion de todxs!!!!
@shivrajbhatti1074
@shivrajbhatti1074 5 жыл бұрын
an example of the difference in functionality at the end would make this 💯🌊
@amin2468
@amin2468 5 жыл бұрын
Excellent tutorial, genious.
@BlueprintBro
@BlueprintBro 3 жыл бұрын
Fantastic explanation!
@qemmm11
@qemmm11 9 ай бұрын
Suddenly! I feel that the pointer arithmetic is so easy😮 Thanks❤
@suryakantpawar6125
@suryakantpawar6125 6 жыл бұрын
it is awesom bro.......i like and understand this concept.....
@AhmedKhan-yw3wx
@AhmedKhan-yw3wx 4 ай бұрын
And here I was thinking pointers would be hard to tackle, thank you so much for such an easy explanation .
@kjelljohanforgard7354
@kjelljohanforgard7354 5 жыл бұрын
Finnaly, someone who explained reference =)
@gregholland11
@gregholland11 2 жыл бұрын
This was helpful. Always wondered what the difference was but never cared enough to Google it.
@fionaho4598
@fionaho4598 2 жыл бұрын
i appreciate your explanation, way better than my university professor!
@jasonjypark
@jasonjypark 4 жыл бұрын
Your explanation was better than my professor's!
@riscaverse
@riscaverse 2 жыл бұрын
Great explanation!!!
@paranormalsoulz
@paranormalsoulz 5 жыл бұрын
this helped me so much for my final in likecan hour thank you!
@Theterry383
@Theterry383 5 жыл бұрын
mine is in 20 hours
What Are Pointers? (C++)
41:55
javidx9
Рет қаралды 561 М.
Ouch.. 🤕
00:30
Celine & Michiel
Рет қаралды 23 МЛН
Can A Seed Grow In Your Nose? 🤔
00:33
Zack D. Films
Рет қаралды 27 МЛН
what even is a "reference"?
5:44
Low Level Learning
Рет қаралды 125 М.
C++ pointers explained easy 👈
5:16
Bro Code
Рет қаралды 12 М.
Comparing C to machine language
10:02
Ben Eater
Рет қаралды 5 МЛН
Call By Value & Call By Reference in C
8:34
Neso Academy
Рет қаралды 1,3 МЛН
why are switch statements so HECKIN fast?
11:03
Low Level Learning
Рет қаралды 393 М.
you will never ask about pointers again after watching this video
8:03
Low Level Learning
Рет қаралды 2,1 МЛН
What does int argc, char* argv[] mean?
10:11
Paul Programming
Рет қаралды 350 М.
C++ Pass by Value, Reference, Pointer Explained
10:18
Caleb Curry
Рет қаралды 40 М.
you will never ask about pointer arithmetic after watching this video
8:01
Low Level Learning
Рет қаралды 237 М.
Ouch.. 🤕
00:30
Celine & Michiel
Рет қаралды 23 МЛН