CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 13 of 13
  1. #1
    Join Date
    Feb 2009
    Posts
    326

    pointer to pointer concept

    Hi,

    I am a newbie to C++, I would like to understand the concept of "pointer to a pointer".

    I do understand the following:
    - A pointer is an address to a variable (even can store the address of the starting element of the array)
    - An array variable is a pointer (contains the address) to the first indexed element of the array
    - A pointer to an array (first element of the array) can access the elements of the array using [ ] notation.

    What I can't understand
    - I can't visualize a pointer to a pointer, what does this mean conceptually, and how can I visualize it.

    I am not looking for the uses of pointer to pointer, more interested in understanding the concept.

    I would like to understand it conceptually before getting into the technicalities of the syntax.


    Thanks,
    Muthu

  2. #2
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: pointer to pointer concept

    Quote Originally Posted by Muthuveerappan
    - An array variable is a pointer (contains the address) to the first indexed element of the array
    Although you can frequently see it in this way, strictly speaking an array is not a pointer. Rather an array decays, or is converted, to a pointer to its first element. A prominent example where the difference between an array and a pointer comes into play is in conjunction with the sizeof operator.

    Quote Originally Posted by Muthuveerappan
    - A pointer to an array (first element of the array) can access the elements of the array using [ ] notation.
    This is true, but it is also true that a pointer to the nth element of an array can access the elements of the array using array notation, except that the "array index" would start from a different offset, n. You could even use p[0] to dereference a pointer to a standalone object, but it is potentially misleading.

    Quote Originally Posted by Muthuveerappan
    - I can't visualize a pointer to a pointer, what does this mean conceptually, and how can I visualize it.
    It means that a pointer is just a variable: just as you can have a pointer point to an int, you can have a pointer point to a pointer to an int.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  3. #3
    Join Date
    Nov 2006
    Location
    Australia
    Posts
    1,569

    Re: pointer to pointer concept

    Alas, the power of MS Paint in action!

    Good judgment is gained from experience. Experience is gained from bad judgment.
    Cosy Little Game | SDL | GM script | VLD | Syntax Hlt | Can you help me with my homework assignment?

  4. #4
    Join Date
    Mar 2009
    Location
    Granada, Spain
    Posts
    40

    Re: pointer to pointer concept

    Quote Originally Posted by Mybowlcut View Post
    Alas, the power of MS Paint in action!

    That looks like 3DStudioMax9+Photoshop CS15+FreeHand23

  5. #5
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: pointer to pointer concept

    Also, it's missing a *.

  6. #6
    Join Date
    May 2002
    Posts
    1,435

    Re: pointer to pointer concept

    I don't know if this will help, but we'll see. Consider this code:
    Code:
     
    char array[4] = { 'a', 'b', 'c', 'd' } ;
    char *p = array;
    char **pp = &p;
    
    [0008] 'a' ( array[0] )
    [0009] 'b' ( array[1] )
    [0010] 'c' ( array[2] )
    [0011] 'd' ( array[3] )
    [0012] 0008 ( p = pointer to array = 0008 )
    [0013] 0012 ( pp = pointer to p = 0012 )
    [0014] ?
    [0015] ?
    Every variable that's declared is stored in memory somewhere. I have shown eight memory locations arbitrarily starting at 0008 and continuing through 0015. For simplicity, assume that pointers are one byte which is not really the case.

    Look first at the declaration of 'array'. It takes up four memory locations and let's say that they they get stored in memory locations 0008 through 0011. The address of array or array[0] is 0008.

    Now define another variable which is a pointer to the array, p. p will be stored at memory location 0012 but its VALUE will be 0008, the address of array or array[0]. Since p holds the same value as the address of array, 0008, they can be used interchangeably.

    Now define another variable, a pointer to a pointer, pp. pp will be stored at memory location 0013 and its VALUE will be 0012 or a the address of p.

    So now you have:

    pp - a pointer with a value of 0012 - so it points to another pointer, p at location 0012.

    p - a pointer with a value of 0008 - so it points to memory location 0008, or array.

  7. #7
    Join Date
    Nov 2006
    Location
    Australia
    Posts
    1,569

    Re: pointer to pointer concept

    I think paint still looks better than max:



    Don't you?

    Also, it's missing a *.
    Oops!
    Last edited by Mybowlcut; March 23rd, 2009 at 07:34 AM.
    Good judgment is gained from experience. Experience is gained from bad judgment.
    Cosy Little Game | SDL | GM script | VLD | Syntax Hlt | Can you help me with my homework assignment?

  8. #8
    Join Date
    Feb 2009
    Posts
    326

    Re: pointer to pointer concept

    Thanks all for replying and helping me understand, I have summed up my understanding, pls correct me if I am wrong.

    Assume the following:
    k1 is a int variable
    p1 is a pointer to k1
    p2 is a pointer to p1

    My initial Doubt
    -------------------
    1) When the address of k1 can be stored in a int* variable, why can’t the address of the pointer p1 be stored in another int* variable ?
    2) What is so special about the address of p1, that it can’t be stored in another int* variable ?
    3) Is the address of p1 not just another address ?


    My understanding
    ----------------------
    There is nothing special about the address of p1, it is just another address.

    Consider the attached picture (see attachment)

    - Here p1 stores the address of k1 and *p1 returns the value of k1 (which is an int value)
    - p2 stores the address of p1 (normal address) and *p2 returns the value of p1 (which is an address not an int value).
    - If p2 was declared as int*, then *p2 would only be able to return an int value (the pointer would point to an integer)
    - Only if p2 was declared as int**, would*p2 be able to return an address.
    - So in order to for *p2 to return an address, we would need to declare p2 as int**
    Attached Images Attached Images  
    Attached Files Attached Files
    Last edited by Muthuveerappan; March 23rd, 2009 at 01:45 PM. Reason: Formatting

  9. #9
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: pointer to pointer concept

    That's more or less it. All pointers require the same amount of memory (4 bytes on 32-bit systems), regardless of what they point to; the type of a pointer (int*, int**, void*, char***********, std::vector<int>*, etc) only determines what the type of "*p" will be, eg, same type as p with one fewer *s. (It's also a bit relevant to pointer arithmetic for similar reasons.)

  10. #10
    Join Date
    Jan 2004
    Location
    Düsseldorf, Germany
    Posts
    2,401

    Re: pointer to pointer concept

    Quote Originally Posted by Muthuveerappan View Post
    My initial Doubt
    -------------------
    1) When the address of k1 can be stored in a int* variable, why can’t the address of the pointer p1 be stored in another int* variable ?
    It could. The below program works just like yours:
    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
        int k1=20, *p1, *p2;
    
        p1 = &k1;
        p2 = (int*)&p1;
    
        cout << "k1  =" << k1  <<  "\t\t&k1 =" << &k1 << endl
             << "*p1 =" << *p1 <<  "\t\tp1  =" << p1  << "\t\t&p1 =" << &p1 << endl 
             << "**p2=" << **(int**)p2 << "\t\t*p2 =" << (int*)*p2 << "\t\tp2  =" << p2  << "\t\t&p2 =" << &p2 << endl;
    }
    However, why do you want to pretend that p2 is pointing to an integer while in fact p2 is pointing to an address. The compiler even warns you that your pointers are not correct, that's why I added the casts.

    BTW, there is a concept of pointers that are not defining what they point to. They are defined as void *. The concept is used, when you have to store a memory address without knowing yet what exactly is at this memory, e.g. in network programming or inter-process communication.
    More computing sins are committed in the name of efficiency (without necessarily achieving it) than for any other single reason - including blind stupidity. --W.A.Wulf

    Premature optimization is the root of all evil --Donald E. Knuth


    Please read Information on posting before posting, especially the info on using [code] tags.

  11. #11
    Join Date
    Feb 2009
    Posts
    326

    Re: pointer to pointer concept

    Thank you so much for all your replies.

    Really helped me understand the concept better.

    I was so caught up with the idea that a pointer variable is capable of storing the address and completely missed the point that using the * or [ ] the pointer is capable of accessing the value of whatever it points to.


    One more Doubt:
    ---------------------
    Thanks for being very patient with me, I have one more doubt (explained below):

    I am a bit confused between the difference a pointer and an array.
    From Laserlight's comment I understand that an array can be converted into a pointer.

    I have not yet covered the sizeof operator yet (I am learning from a book). So hopefully will cover it later.

    My understanding is that a pointer is an address of a variable.
    An array is a series of memory locations, and the array variable contains the address of the first indexed variable.
    So does that not make an array variable a pointer or is it just a representation ?

    Is an Array like a special pointer, meaning it is more than a normal pointer or is it just not a pointer but can be converted into a pointer ?

  12. #12
    Join Date
    Jan 2008
    Location
    California, USA
    Posts
    822

    Re: pointer to pointer concept

    I think the explanation 0xC0000005 gave is an excellent reference.
    Is an Array like a special pointer, meaning it is more than a normal pointer or is it just not a pointer but can be converted into a pointer ?
    I don't know the in-depth of how pointers are implmented, but it is enough for you and me to look at the pointers as the "iterators" for arrays.
    So does that not make an array variable a pointer or is it just a representation ?
    I think former is more correct way of looking at it.
    I have not yet covered the sizeof operator yet (I am learning from a book). So hopefully will cover it later.
    My understanding of the usage of sizeof in pointer is that it is used in a way that allow us to do pointer arithmetic. For example, if we have an array of T, then we can take sizeof(T) to traverse a number of bytes in a memory.

    hope this helps

  13. #13
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: pointer to pointer concept

    Quote Originally Posted by Muthuveerappan View Post
    My understanding is that a pointer is an address of a variable.
    An array is a series of memory locations, and the array variable contains the address of the first indexed variable.
    So does that not make an array variable a pointer or is it just a representation ?

    Is an Array like a special pointer, meaning it is more than a normal pointer or is it just not a pointer but can be converted into a pointer ?
    It's probably best to think of an array as a pointer that comes with some bit of stack memory and can't be reassigned. It's slightly more complicated than that, but thinking of it that way will give you the right intuition.

    That way when you start directing pointers at dynamic arrays on the heap, the parallel will be clear.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured