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.
- 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.
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.
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
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.
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**
Last edited by Muthuveerappan; March 23rd, 2009 at 01:45 PM.
Reason: Formatting
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.)
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:
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
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 ?
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.
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.
* The Best Reasons to Target Windows 8
Learn some of the best reasons why you should seriously consider bringing your Android mobile development expertise to bear on the Windows 8 platform.