What is the Difference Between Pass By Pointer and Pass By Pointer Reference (int * and int * &) C++

แชร์
ฝัง
  • เผยแพร่เมื่อ 23 ธ.ค. 2024

ความคิดเห็น • 127

  • @sheheryarwasti1426
    @sheheryarwasti1426 8 ปีที่แล้ว +145

    I really like the way you teach. It's concise, it's visual and you explain it so anyone can can understand it. Thank you! -just a fan

    • @PaulProgramming
      @PaulProgramming  8 ปีที่แล้ว +11

      +Sheheryar Wasti I'm glad you like it

    • @A7medMSD
      @A7medMSD 7 ปีที่แล้ว +1

      but why you stopped :/ ...

    • @bobsmithy3103
      @bobsmithy3103 6 ปีที่แล้ว +1

      Agreed. Even myself with no experience with c++ could follow along like a breeze.

    • @crptc5707
      @crptc5707 4 ปีที่แล้ว +1

      @@PaulProgramming Until today I have learned that c++ has true pass by reference, I always think java and c pass by pointer value is the same as pass by reference!

    • @jcrackerjcrack6401
      @jcrackerjcrack6401 4 หลายเดือนก่อน

      I'm grateful your videos are still up. I would take a full structured course from you.
      Hope whatever you're doing it's fun!
      Thanks, take care

  • @zush2305
    @zush2305 5 ปีที่แล้ว +40

    finally someone has explained this, it was really confusing to study this and everyone was explaining the difference between reference and value, took me a while to find the subject i was looking for

    • @crptc5707
      @crptc5707 4 ปีที่แล้ว +1

      that's true, I've learned today that unlike pointer, reference variable doesn't occupy memory space, which is very shocking stackoverflow.com/questions/1179937/how-does-a-c-reference-look-memory-wise/38310081

  • @zal9706
    @zal9706 6 ปีที่แล้ว +5

    You really have a knack for teaching, you made me understand pointers in 12 minutes more than I did the first week I learned it. Thanks man, you're brilliant.

  • @UpcycleElectronics
    @UpcycleElectronics 6 ปีที่แล้ว +2

    This upload was suggested in my home feed yesterday evening. I saved it in my watch list and after watching, Subscribed for more like this. It was probably suggested bc I was searching for info about pointers a few days ago. ...Still I like to share the hidden metrics behind how I came across a channel when I subscribe. Most CC's go through their upload metrics but it's hard to assess the figures independently ;)
    I'm Jake BTW, an electronics hobbyist. I don't care if you check out what I do. I'm not monetized nor do I plan to be. My playlists are a form of personal notes on electronics. This upload has been added to my "Programming C" playlist. All of my playlists (and subscriptions) are listed publicly for others that might find a compilation of references indexed by subject helpful. I only add what I watch, find helpful, and want to save a reference to.
    This upload helped me understand pointers a little better, but my initial curiosity from a few days ago is still unanswered. When it comes to hardware on a microcontroller, how does a pointer work with an input/output pin?
    For example, if an interrupt happens once, a function is called that sets pointers A, B, and C, to pins 1, 2, and 3 to check boolean input state. Then it does a bunch of magic. If the interrupt happens again pointers A, B, and C are set to pins 4, 5, and 6, and...magic etc.
    At the end of the magic function the outcome is an action using a pointer to an output pin.
    If that made any sense... my main problem, I guess is, what is an address in relation to a hardware I/O. If I create a pointer to an I/O does that copy the current state of the I/O into a memory location, or is an I/O pin simply another address like any other?
    Anyways...you don't need to answer that. It's just something to think about. I haven't found a solid answer to that one yet while searching for info about pointers. I'm probably asking the wrong questions but if I'm looking for this info perhaps others are as well, and if content were made to address it others would find it helpful ;)
    -Jake

  • @SatishSingh-cp3wo
    @SatishSingh-cp3wo 2 ปีที่แล้ว +3

    Hey Paul. This is the first video of yours that i have watched and i absolutely loved it. This is absolutely the way how programming concepts must be taught. A big ❤️

  • @dennismckenzie8329
    @dennismckenzie8329 4 ปีที่แล้ว +1

    You did a great job of explaining this. I read a C++ book several years ago, and I got pointers and references, but when it got into references to pointers, pointers to references, references to pointers of references, etc., my eyes glazed over. It all went completely over my head. Since then, when I encountered this, I would just skip it. However, this explained it so well. Thank you!

  • @h3ll3r
    @h3ll3r 4 ปีที่แล้ว

    I like your teaching style with bunch of visualisation. The best explanation that I've ever seen, thank you for your work!

  • @cicartaya
    @cicartaya 2 ปีที่แล้ว

    😮I... I'm in awe of how beautiful this explanation was. It's a work of art. Masterful. Just a few minutes into it I had to like and subscribe. This is truly an amazing gift you've given everyone. I'm eternally grateful. Thank you for this video.

  • @exitplanetduster
    @exitplanetduster 3 ปีที่แล้ว

    One of the best lesson I found on internet, very clear and complete, compliments.

  • @mohamadiqmalbinjamaludin2160
    @mohamadiqmalbinjamaludin2160 4 ปีที่แล้ว

    I just have seen 2 videos of your tutorial and subscribed immediately. Super clear, concise explanation. Keep teaching.

  • @johnnycashcow1130
    @johnnycashcow1130 4 ปีที่แล้ว +2

    For the confused, here's more of an explanation using the code he used in the program (Note: it helps if you have a clearer grasp of pass by value and by pass by reference):
    I want to start out by saying, and I wish he clarified this in the video, that he initialized gptr as a global variable and that is why he is able to access and use it in the functions.
    Pass by Pointer:
    You begin by passing an int pointer variable into passByPtr because the argument that the function accepts is that type. Let's pass int* p into the function. So that looks like:
    passByPtr(p);
    The function, passByPtr(int * ptr), creates a new object. The new object is ptr. And the contents of p is copied into ptr. P AND PTR ARE SEPERATE OBJECTS. However, they have access to the same memory address. The advantage of this is you could dereference ptr and it would change the contents of p. But if you change the memory address of ptr, there would no difference. So in the function if you did this:
    *ptr = 43; // you dereference by using the * operator in front of the variable
    // when you dereference a pointer, you access the value at that memory address
    A pointer is just a variable that holds a memory address so you are just creating a new object with the same memory address. When the function ends, ptr goes out of scope and wiped off the top of the stack.
    Pass by Pointer Reference:
    You begin the same as you would by passing a pointer. Let's pass p into passByPtrRef(int * & ptrRef). The difference now is that in passByPtrRef(int * & ptrRef) , the function DOES NOT create a new object. ptrRef and p are the same object. ptrRef is just an alias to p. Any changes made to ptrRef will be applied to p.

    • @cratergaje
      @cratergaje ปีที่แล้ว

      But if the parameter is just (int &ptref) what difference would it make?

  • @rw7154
    @rw7154 8 หลายเดือนก่อน

    You have a very lucid style of explanation! You earned a new subscriber Sir.

  • @countesscarmilla1479
    @countesscarmilla1479 10 หลายเดือนก่อน

    WOW. I will be coming back to this video several times and recommending it to my friends. Thank you.

  • @That1Guy248
    @That1Guy248 6 ปีที่แล้ว

    Best coding channel i've found yet. Will definitely be using it for the remainder of my coding course.

  • @frankoppongkonadu6676
    @frankoppongkonadu6676 2 ปีที่แล้ว

    Thank God, I finally found the video I'm looking for, God bless you bro, you save my stress

  • @thisaintmyrealname1
    @thisaintmyrealname1 7 ปีที่แล้ว +1

    Man these videos are amazing. Some people might find that you went a bit too fast on this one but I saw the video on the difference between passing by value, passing by reference and passing by pointer, just before this one, and this all felt like a very natural 2nd part, with no need to re-write the code from scratch, as you did in that other video. Maybe you should label them as part 1 & part 2 so people don't get lost. Anyway great video, I was getting those concepts confused. Thanks Paul.

  • @somsk56
    @somsk56 ปีที่แล้ว

    great concept ❤
    I have been searched this type of video for 5 days... You just make me cry 😭 dude

  • @sirshallot3036
    @sirshallot3036 4 ปีที่แล้ว

    A simple explanation and yet better and more concise than many other explanations. Thank you

  • @Byynx
    @Byynx 3 ปีที่แล้ว

    Very good. We just need the right person to explain. Thanks !!!

  • @Kino-Imsureq
    @Kino-Imsureq 4 ปีที่แล้ว

    This video and your other video, it explained pointers and references way better than other sources.

  • @MovieRecapShow
    @MovieRecapShow 9 หลายเดือนก่อน +1

    It was a really wonderful explanation, Thank you Paul!!

  • @ahmedjemaii3160
    @ahmedjemaii3160 4 ปีที่แล้ว

    i really like your tutorial. It's very simple very basic. I didn't find anything better than this tutorial even in Udemy.Thanks !!!

  • @davidyos2575
    @davidyos2575 6 ปีที่แล้ว +1

    a clear explanation and visualization for a complex subject. Well done and thank you!

  • @moisascholar
    @moisascholar 4 ปีที่แล้ว +16

    Summary: Passing in a pointer by copy/value means that you "clone" the pointer of interest, and you will not be able to change what the original pointer is pointing to (certainly you can change values of what it is pointing to...but not the pointer itself...). If you pass by pointer reference (physically passing in the pointer itself), then you may change what the pointer is pointing to. Hope this helps.

    • @MirrorsEdgeGamer01
      @MirrorsEdgeGamer01 4 ปีที่แล้ว +2

      Mo Aboulmagd Thank you for this, your summery really helped understand this topic and the video better.

    • @moisascholar
      @moisascholar 3 ปีที่แล้ว +1

      @Jay Jay To avoid making a copy. Useful for deletion, to avoid having a dangling pointer, or a pointer pointing to some undefined junk (assuming you pass a pointer by copy and call delete on what it is pointing to), and that object is dynamically allocated of course. After calling delete on a pointer, for best practices you should set that pointer to be equal to nullptr. This is of course only useful if the pointer can still be accessed at a later time (was passed by copy and not by reference) in the execution of the program, but if that is not the case, then setting the pointer to nullptr is unnecessary.
      Passing a pointer by reference should be used judiciously, really only when you are going to be calling delete on an object allocated on the heap that the pointer is pointing to. If you don’t call delete and assign the pointer to some other object (stack or dynamically allocated, doesn’t matter), then you will suffer a memory leak if the object that the pointer was originally pointing to was dynamically allocated (on the heap). You can avoid having the responsibility to call delete by using smart pointers in modern C++ (C++11 and later standards).

  • @thestarinthesky_
    @thestarinthesky_ 4 ปีที่แล้ว

    That illustration has helped me a lot to understand the concept. Thank you so much.

  • @AthitiaPrakash
    @AthitiaPrakash หลายเดือนก่อน

    Thanks for making a best lecture of visualizing a pointer

  • @Skalamambo1298
    @Skalamambo1298 2 ปีที่แล้ว +1

    So a pointer reference is like a reference (a reference is like creating an alias/nickname of a variable/identifier). A pointer reference is like creating an alias or nickname of the pointer (a pointer is creating a memory address that points to whatever it points to).

  • @Aymanoslexilogos
    @Aymanoslexilogos ปีที่แล้ว

    Best explanation of pointers in C in my opiniok. Thank you

  • @wickpoker
    @wickpoker 6 ปีที่แล้ว +6

    Outstanding explanation! Thank you!

  • @ARANDOMOPENAIUSER
    @ARANDOMOPENAIUSER ปีที่แล้ว

    Fantastic tutorial, helped me a lot with understanding pointers. Thank you

  • @JigarPatel28
    @JigarPatel28 6 ปีที่แล้ว +3

    can you please make more videos on specifically: queues, graphs & graph traversals, tries, sorting algorithms(merge sort, quick sort, insert sort, heap sort, etc), and big-o notation?

  • @cestmacassette9033
    @cestmacassette9033 3 ปีที่แล้ว

    Like the way you visualize lession, respect (y)

  • @viktornikolov1570
    @viktornikolov1570 3 ปีที่แล้ว

    This was a REALLY helpful video. Thank you Paul.

  • @raiyanreza9764
    @raiyanreza9764 5 ปีที่แล้ว

    one of the best teachers!

  • @u3k1m6
    @u3k1m6 6 ปีที่แล้ว

    Thanks for the tutorial Paul. This video was extremely helpful and I think I understand it a lot more now.

  • @prasunakolla2427
    @prasunakolla2427 4 ปีที่แล้ว

    Thank you so much. Your explanation was very nice. It helped me a lot.

  • @vincenr8822
    @vincenr8822 6 ปีที่แล้ว +1

    Underrated video! Needs more views ;)

  • @MoMoeMedia
    @MoMoeMedia 8 ปีที่แล้ว +6

    Awesome video! Could you do a video on double pointers? I really like your teaching style.
    EDIT: Also, what is the different between using this rather than using a double pointer?

  • @maanillaad4324
    @maanillaad4324 4 ปีที่แล้ว

    thanks a lot man! was stuck on this one, cleared all my doubts

  • @ergingokkaya8323
    @ergingokkaya8323 5 ปีที่แล้ว +1

    Thanks a lot for great explanation.. one silly question.. a call by reference function signature is like that: void foo(int& x).. considering this, should passbyrefptr be like that: void passbyrefptr (int& * ptr).. because former says reference to a integer variable and the latter ,i think, should say reference to a int pointer.. i confuse the syntax a lot

  • @mycomputativeromance4266
    @mycomputativeromance4266 3 ปีที่แล้ว

    really really thank you guy! clear and impressive. that's really helps me

  • @alseynidiop8650
    @alseynidiop8650 5 ปีที่แล้ว

    Best explanation of this subject. Thanks

  • @yauck1
    @yauck1 6 ปีที่แล้ว +13

    11:50 my brain starts to overclock... good explanation, I have to watch the last 30 seconds 3 times in slow mo to get it... LOL

  • @Olavotemrazaodenovo
    @Olavotemrazaodenovo 4 ปีที่แล้ว

    Congratulations from Brazil.

  • @amolgaikwad4114
    @amolgaikwad4114 4 ปีที่แล้ว

    Great!!!! Excellent explanation and concept cleared... Thank you....

  • @frankie_goestohollywood
    @frankie_goestohollywood 2 ปีที่แล้ว

    Thank you ! Excellent tutorials.

  • @wlo2661
    @wlo2661 4 ปีที่แล้ว

    gooood before my exam good content thumbs up

  • @m.alaiady3627
    @m.alaiady3627 4 ปีที่แล้ว

    Thank you sir ! that's really helpful
    Is there a video in your channel talk about the *_heap_* and when should I use the *_new_* keyword ?!

  • @manugomez7319
    @manugomez7319 3 ปีที่แล้ว

    At 10:53. But a reference cannot be made to point to another variable isnt it?

  • @hashinology
    @hashinology 7 ปีที่แล้ว

    Paul you are my man finally i understand pointer and the fucking reference

  • @DipsAndPushups
    @DipsAndPushups 2 ปีที่แล้ว

    This is how my understanding of why Java is pass by value. If you pass a reference to a method in Java, that method receives a copy of the reference, a copy of the memory address, while in C++ you have the pass by reference option where your function or method can actually manipulate the variable that you have passed as an argument instead of getting a copy of that variable.
    P.S. Please indent the code.

  • @looploop6612
    @looploop6612 3 ปีที่แล้ว

    love you graph !

  • @shovershover
    @shovershover 2 ปีที่แล้ว

    Very clear! I like this video

  • @williamtian566
    @williamtian566 5 ปีที่แล้ว

    great lesson , i really understand and enjoy it . thank u so much !

  • @bardock-yk2xg
    @bardock-yk2xg 3 ปีที่แล้ว

    Terrific explanation.

  • @kesavkosana9818
    @kesavkosana9818 3 ปีที่แล้ว

    visuals are extremely helpful thank you

  • @mulimotola44
    @mulimotola44 8 ปีที่แล้ว

    Great and clear explanation! Thank you very much!

  • @adamodimattia
    @adamodimattia 5 ปีที่แล้ว

    Great lesson, this one and others, so clear! Thank you.

  • @nizarouertani1315
    @nizarouertani1315 4 ปีที่แล้ว

    is it like double pointers where you can change what the pointer it self contain ???? i think it's not the same cause refrence looks to me is manupulating the pointer by accessing him but double pointers is creating a variable wich will contain the address of the pointer than you can access it

  • @davidlira8853
    @davidlira8853 2 ปีที่แล้ว

    i like the breakdown of steps

  • @ichias16
    @ichias16 6 ปีที่แล้ว

    really good tutorial about a confusing topic :D helped a lot

  • @geezeer00912
    @geezeer00912 7 ปีที่แล้ว +1

    Awesome videos man,very helpful,thank you.

  • @hubercats
    @hubercats 3 ปีที่แล้ว

    Very helpful video. Thank you!

  • @PCHerc
    @PCHerc 2 ปีที่แล้ว

    This is an excellent video.

  • @quannguyenhahong168
    @quannguyenhahong168 ปีที่แล้ว

    Great explanation, thanks !

  • @mcho850
    @mcho850 6 ปีที่แล้ว +1

    Hey 1 quick question, whats the difference between pointer to pointer and pointer reference? both of it seems to did the same thing by manipulate the address of pointer pointing to and the data of the pointer is pointing to

  • @StevenWernerCS
    @StevenWernerCS 7 ปีที่แล้ว +2

    is this different for objects? my own struct for instance

  • @ilya4068
    @ilya4068 2 ปีที่แล้ว

    awesome explanation!!! thanks dude

  • @rey.rosario7318
    @rey.rosario7318 3 ปีที่แล้ว

    Nice tutorials, is there free software to illustrate as you in these examples with boxes and arrows sir?
    Would help me to draw and follow me own examples and learn to use it on my own pc.
    Thank you.

  • @mhcbon4606
    @mhcbon4606 5 ปีที่แล้ว

    put at 1.5x speeed. The explanation and the pronunciation are easy to understand. I d like to know which tools are being used to produce the drawings.

  • @lssangpi
    @lssangpi 7 ปีที่แล้ว

    This is so helpful! Thanks!

  • @marsellusrex
    @marsellusrex 6 ปีที่แล้ว

    Amazing lesson, thank you!

  • @usama282
    @usama282 4 ปีที่แล้ว

    Can please somebody reply . In the pass by pointer function if we pass parameter as address of ptr, does it achieve same thing as function with pointer reference
    what i am trying to ask is Does PassByPtr(&p) do same thing as PassbyPtrRef(p) ??

  • @dustinhartlyn
    @dustinhartlyn 2 ปีที่แล้ว

    Thanks you, well explained!

  • @ThanhTrantrancongthanh
    @ThanhTrantrancongthanh 8 ปีที่แล้ว

    could you explain return value of some std operators? e.g. ++ & -- (postfix & prefix), = (assignment)...

  • @m.alaiady3627
    @m.alaiady3627 4 ปีที่แล้ว

    So the reference pointer is more efficient ?

  • @sleepdeprived3678
    @sleepdeprived3678 2 ปีที่แล้ว

    Thank you, great video.

  • @williamchamberlain2263
    @williamchamberlain2263 6 ปีที่แล้ว

    5:50 - "Oh", said I, as an ex-Java developer: "I forgot that the pointer is copied into the argument".

  • @nilupulperera
    @nilupulperera 6 ปีที่แล้ว

    Very interesting. Thank you very much.

  • @Gornius
    @Gornius 4 ปีที่แล้ว

    I was wondering why I was getting a garbage data by making a function to allocate memory for array by passing its pointer (first element) to a function. It took me some time and rewriting this function in C to understand what was happening in the background.

  • @shivkumarpippal1765
    @shivkumarpippal1765 8 ปีที่แล้ว

    Thanks. This was very helpful!!!

  • @rosengeorgiev6817
    @rosengeorgiev6817 8 ปีที่แล้ว

    Great video Paul!
    Will you make a tutorial about smart pointers too?

  • @MohamedSayed-wl5cj
    @MohamedSayed-wl5cj 3 ปีที่แล้ว

    amazing visualization

  • @riufq
    @riufq 6 วันที่ผ่านมา

    But why you declare gptr outside the function but p is not?

  • @carlpeterkirkebo2036
    @carlpeterkirkebo2036 ปีที่แล้ว

    Great stuff!
    Thanks!

  • @theonionpirate1076
    @theonionpirate1076 6 ปีที่แล้ว

    this is a great video, new sub

  • @rileynobles7146
    @rileynobles7146 4 ปีที่แล้ว

    Nice explanation

  • @kal5211
    @kal5211 3 ปีที่แล้ว +1

    My man.

  • @Gamerzhut
    @Gamerzhut 3 ปีที่แล้ว

    @Paul Programming , dude please make a video of pass by pointer but with functions with a dynamic 2D array , ill be very happy if you make 1 little video , nice video though, keep up :)

  • @sushmapanwar3712
    @sushmapanwar3712 4 ปีที่แล้ว

    Which IDE is it?

  • @Recordingization
    @Recordingization 2 ปีที่แล้ว

    Good lecture

  • @MrFallred
    @MrFallred 2 ปีที่แล้ว

    Bless you.

  • @hrishikeshkashyap3715
    @hrishikeshkashyap3715 5 หลายเดือนก่อน

    Can you make another video elaborating the underlying mechanism behind *passByPtrRef* .... I just wanna be honest. I'm finding it very difficult to understand this concept.

  • @pranaykothari571
    @pranaykothari571 2 ปีที่แล้ว

    Awesome video

  • @thisaintmyrealname1
    @thisaintmyrealname1 7 ปีที่แล้ว

    what text editor do you use?

  • @caio-jl6qw
    @caio-jl6qw ปีที่แล้ว

    Thank you!!

  • @TA-mz2ol
    @TA-mz2ol 4 ปีที่แล้ว

    very clear

  • @ludmilalutencu
    @ludmilalutencu 5 ปีที่แล้ว

    SUPER video

  • @isaacRdzC
    @isaacRdzC 7 ปีที่แล้ว

    Very Helpfull :)

  • @Fahodinho
    @Fahodinho 3 ปีที่แล้ว

    Excellent