Thread: References in C(in aspect of pointers)

  1. #1
    Banned
    Join Date
    Apr 2015
    Posts
    596

    References in C(in aspect of pointers)

    Hi guys;
    I'm just wondering how actually the address of memory location can be referenced by multiple vatiables; in other words I'm a little baffled about the idea that multiple variables refering to the same value which means they referred to the
    "same address" ; I know that subject "references" found in world of programming; but how can I relate this as analogus to real life? I don't want to just copy paste how references are working in programming; also want to be freshy with them by real life analogus!!


    And if could also explaining what the term "reference to" mean in aspect of programming?



    Thanks in advance and hope to help me guys!

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,728
    I used that analogy before, when I was trying to explain how the copy of a pointer doesn't copy the data it points to:

    Think of a pointer, aka a reference, as a treasure map. A treasure map tells you where to find a treasure. You can have many identical maps that would naturally send you to the same treasure. If you use one of those identical maps to go to that treasure and change it in some way, any other identical map would take you to that modified version, because they all point to the same treasure.

    In a similar fashion, you can have a treasure map that points to the location of another treasure map which points to a treasure. That would be the analogy for a double pointer. I hope you see where I'm going with this. In reality, a pointer is just a variable that holds a memory address. You can dereference it in order to go to that memory address and see and/or modify its contents.
    Devoted my life to programming...

  3. #3
    Banned
    Join Date
    Apr 2015
    Posts
    596
    Quote Originally Posted by GReaper View Post
    I used that analogy before, when I was trying to explain how the copy of a pointer doesn't copy the data it points to:

    Think of a pointer, aka a reference, as a treasure map. A treasure map tells you where to find a treasure. You can have many identical maps that would naturally send you to the same treasure. If you use one of those identical maps to go to that treasure and change it in some way, any other identical map would take you to that modified version, because they all point to the same treasure.

    In a similar fashion, you can have a treasure map that points to the location of another treasure map which points to a treasure. That would be the analogy for a double pointer. I hope you see where I'm going with this. In reality, a pointer is just a variable that holds a memory address. You can dereference it in order to go to that memory address and see and/or modify its contents.
    I got your analogy but still confused how actually can multiple variables having the same value aka @ the same address @ and those two variables lead to the concrete value of the address they have stored .. !
    what's really baffled me how "one" value can be shared commonly to multiple variables?!

    the term "reference to" mean in programming means that they have the same value, i.e X reference to BANK; implicitly mean that the value of x is BANK, right?! -- BANK is just any defined enum variables. -- and once again MAYBE I'm misunderstanding the term "reference to" !

    thank you very much
    Last edited by RyanC; 12-09-2018 at 04:32 AM.

  4. #4
    Informer -Adrian's Avatar
    Join Date
    Jan 2013
    Posts
    817
    I'm a little baffled about the idea that multiple variables refering to the same value which means they referred to the
    "same address"
    Code:
    // same value
    int a = 5;
    int b = 5;
    int c = 5;
    
    int target;
    // same value
    int* p1 = ⌖
    int* p2 = ⌖
    int* p3 = ⌖
    What's so hard to understand? I doubt your endless abstract musings (it's been years now?) will turn into expertise some day. You ought to be working on concrete problems and gain practical insight.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pointers and references?!? Help!
    By alyeska in forum C++ Programming
    Replies: 3
    Last Post: 10-30-2008, 10:23 AM
  2. Pointers/references
    By RobotGymnast in forum C++ Programming
    Replies: 13
    Last Post: 09-19-2008, 12:03 PM
  3. Pointers v References
    By m37h0d in forum C++ Programming
    Replies: 28
    Last Post: 06-30-2008, 01:29 PM
  4. Pointers and References
    By Hetcenus in forum C++ Programming
    Replies: 11
    Last Post: 06-16-2006, 09:26 AM
  5. Pointers and references...
    By SushiFugu in forum C++ Programming
    Replies: 6
    Last Post: 12-08-2001, 04:21 PM

Tags for this Thread