Thread: Is this analogy for pointers (*operator), addresses (& operator) and variables valid?

  1. #1
    Registered User
    Join Date
    Dec 2016
    Posts
    45

    Is this analogy for pointers (*operator), addresses (& operator) and variables valid?

    I was wondering if this is the right way to think about pointers (*operator), addresses (& operator) and variables. I'm giving a simplified explanation in the black boldface text, and a more technical one in red.

    I'm trying to create a simplistic mental map in my head so I can instinctively remember what all these components actually are. (Sort of like a pneumonic device). I'm having trouble wrapping my head around the relationships between the three concepts


    -A pointer variable---which is denoted by the *operator (indirection)---is like an arrow that can point to an address. It is technically a variable that can store an address. You will have to initialize it to an address with the &operator for it to point to something.

    -An address---which is denoted by the &operator---is like the physical location that stores a value of the variable of interest to the user. It corresponds with a physical byte location in memory.

    -A variable is the data (of a certain type---int, float, char, etc) that the user wants to store. A sequence of 1's and 0's located at a physical address in memory
    Last edited by potomac; 12-25-2016 at 12:37 AM.

  2. #2
    Registered User
    Join Date
    Dec 2016
    Posts
    45
    This was the text from my book that I'm basing my simplified mental map on:



    (page 207)
    The * operator relates to pointer variables. The act of declaring a pointer variable sets aside memory space for a pointer, but it doesn't make it point to an object. In this respect, pointers are no different than other variables

    int *p; /* points nowhere in particular */


    It is therefore crucial to initialize that variable the pointer p (which is denoted by *p) before we use it. You can assign it the address of some lvalue by using the & operator. That is, the & operator allows you to aim the pointer at the address of an object.

    int i;
    int *p;

    p = &i;







    (page 208)

    You can think of the * operator as the inverse of the & operator

    -Applying & to a variable produce a pointer to the variable

    -Appling * to the pointer takes us back to the original variable
    Last edited by potomac; 12-25-2016 at 12:21 AM.

  3. #3
    Old Took
    Join Date
    Nov 2016
    Location
    Londonistan
    Posts
    121
    A pointer is an address.
    Code:
    int *ptr_num, num;
    // ptr_num is of type pointer to int, num is of type int. Illustration of why not do declare multiple variable at same time perhaps
    ptr_num = # // until here ptr_num would have been some random address, now stored is the addres of num
    num = 5; // you know this.
    *ptr_num = 6; // now num = 6, we dereferenced the pointer with the dereference symbol. This says I want what is pointed at, not the pointer
    // itself. ptr_num is the address of num. It adds a layer of indirection.

  4. #4
    Registered User
    Join Date
    Dec 2016
    Posts
    45
    ^ I think I should have written pointer variable in my title?

    Is there a difference between a pointer variable and a pointer?

    Does a pointer consist of a pointer variable + an address?

    I've seen the author of my book use both terms to use different things scattered throughout the chapter.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    How about.

    int myhouse;
    int *postcard = &myhouse;

    A pointer variable is like a postcard. It's a fixed sized thing you can write any address on (regardless of whether the building is a house or a skyscraper).

    If you want to tell someone where you live, you send them a postcard. You don't make a complete copy of your house and send it to them.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Old Took
    Join Date
    Nov 2016
    Location
    Londonistan
    Posts
    121
    Quote Originally Posted by potomac View Post
    ^ I think I should have written pointer variable in my title?

    Is there a difference between a pointer variable and a pointer?

    Does a pointer consist of a pointer variable + an address?
    The terms 'pointer variable' and 'pointer' are interchangeable.

    No a pointer is a variable like any other. The only difference is rather than store data there, we store the address of some data. To get to the data being pointed at the pointer has to be dereferenced with the * operator.

    In C all parameter passing is 'by value'. This means that when you pass something to a function, the function actually gets a copy of data passed.
    Code:
    void foo( int a )
    {
       a = 5;
    }
    
    int main()
    {
       int num = 0;
       foo(num);
       // what is num here??
       return 0;
    }
    Where the comment is num has two possible values, they are 0 or 5. Which is it and why?
    It's 0. In main we set num to zero and then we pass a copy of num to foo, so the changes made in foo to num are not reflected back to main. foo was never working with num, it was working with a copy of num.
    Code:
    void bar( int* ptr )
    {
       *ptr = 5;
    }
    
    int main()
    {
       int num = 0;
       bar(&num); // bar expects type pointer to int passed to it, so we pass the address of num
       // what is num here?
       return 0;
    }
    here again the same two possible values for num at the comment are 0 or 5. This time it's 5. Why?
    Again we pass a copy ( all passing in C is 'by value' ). Our pointer is copied and passed to bar. in the function the copied pointer is dereferenced to get to the object being pointed at, and the assignment is made to that memory location. We have just simulated passing by reference by passing the address of an int rather than by passing the int itself. Because we passed an address the changes we made to the object at that address percolate back up to main. This is what Salem means by don't copy your house, send the address of your house.

  7. #7
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    A value is a chunk of memory interpreted as a particular type.
    A variable is a named address.
    A pointer is a variable whose value is interpreted as an address.
    That's it.
    Your analogies are useless.
    Why add a layer of obfuscation over such an incredibly simple concept?

  8. #8
    Registered User
    Join Date
    Dec 2016
    Posts
    45
    Thanks for the responses again guys. Very helpful. Salem's mail analogy was good. Hobbit, I get why pointers are useful now thanks to your elaboration (so you can avoid relying on copying a variable when passing it to a function; just update the original variable itself).

    In hindsight, algorism's simple explanation makes sense now too that I thought about the analogy to better understand the concept

    I found this YouTube video really helpful too. Right after I watched it, I scrolled down to the comments and found a lot of praise. Many are saying its the best video explanation they've found so far.

    Pointers - YouTube
    Last edited by potomac; 12-26-2016 at 07:53 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 31
    Last Post: 05-26-2016, 05:21 PM
  2. Ternary Operator to choose which operator to use
    By cncool in forum C Programming
    Replies: 7
    Last Post: 06-27-2011, 01:35 PM
  3. Replies: 3
    Last Post: 12-09-2008, 11:19 AM
  4. Replies: 2
    Last Post: 07-07-2008, 03:46 AM
  5. Replies: 1
    Last Post: 07-07-2008, 03:38 AM

Tags for this Thread