Click here to Skip to main content
15,878,959 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
There could exist a pointer to a pointer to a pointer to a pointer to a pointer.. and so on.
It would be so confusing to use/handle them. Is there any practical use where it is really beneficial?
Why would we need to store a pointer variable's address into some another pointer variable and so on?
Please advise.
Posted
Updated 12-Dec-14 13:43pm
v2
Comments
PIEBALDconsult 12-Dec-14 20:10pm    
Yes. If you don't know why, stay away from C.
Sergey Alexandrovich Kryukov 13-Dec-14 0:24am    
I would not say its about C. Please see my answer.
—SA
Paulo Zemek 12-Dec-14 20:32pm    
Normally you use pointer to objects...
For some reason, you want to use a input parameter as output (COM, for example, uses HRESULT to indicate success/error, so any result is a pointer to a variable that will hold the real result). When that variable is already a pointer, you end-up with a pointer to a pointer.

More than that is rarely justified but can happen (for valid or invalid reasons). For example, imagine that the first member of a linked list is a pointer to the next item. Being the first item, someone may prefer to access the 5th item by dereferencing a pointer to a pointer to a pointer to a pointer to a pointer instead of using: linkedListFirst->next->next->next->next or some method like linkedListFirst->getItemByIndex(4) // 0 based index.
Legor 16-Dec-14 3:32am    
There are several cases where you need pointer-to-pointer. Pointer-to-pointer-to-pointer (or more) is very uncommon.

There are in fact situations, where it is very handy to have a pointer to a pointer. Here is an example: Lets say you have a linked list and the anchor of the list is somethings like:
C++
struct ListElement* pFirstElement;

Now imagine you want to pass that pointer to a function and give the function the chance to modify the contents of pFirstElement. Then you would pass a pointer to that pointer:
C++
void AddElement (struct ListElement** pAnchor, struct ListElement* pElement)
{
   ...
}

And there are cases where you need a pointer to a pointer to a pointer, but they are rare. But see, if you design a programming language and allow pointers to pointers, then that implies that you can have pointers of arbitrary depths. In fact, in the example given by Paulo Zemek you can see, that the pointer depths can get quite large - although nobody would use that in normal circumstances.

I hope that answered your question.
 
Share this answer
 
Comments
Maciej Los 14-Dec-14 10:41am    
5ed!
A programming language is a tool that gives the programmer the power and also the responsibility to do meaningful things. Like with a hammer: it's supposed to drive nails, not to throw through glass windows. ;-)

So, C/C++ pointers are there to point to memory locations. The memory can be regarded as a large contiguous array of bytes, the bytes in that memory are numbered from 0 to the end of the memory. That number is called the address of a byte in memory.

A variable occupies one or more consecutive bytes in memory to store the variable's value. The address of the variable is the address of the first byte of the variable.

When you declare a variable, the compiler adds the machinery to access that memory location of the variable: if you assign a value to the variable, the program determines the memory location of the variable and stores the value to the bytes at that location. Likewise when you read from a variable: the bytes at that memory location are interpreted as the value of that variable (of a given type).

This address machinery is hidden to the programmer. But under certain conditions, you need the address of a variable instead of the value of the variable. In that case, you define an additional variable that can hold the address of another variable. Such a variable is called a pointer variable. E.g.
C
int number = 5;
int *pointerToNumber = &number;
The * tells that we are not storing an int but rather the address to an int variable in that variable. To access the address of a variable, you put a & before the variable of choice.

Since a pointer variable is also a variable, you can take the address of that variable again, etc.

Use cases in C:

Swapping the value of two variables:
- You must pass the address of the variables.
- If the variable's values were passed directly, you could not exchanged them in memory.
C
void swap_int(int *a, int *b) { int i = *a; *a = *b; *b = i; }
...
int a = 7;
int b = 13;
...
swap_int(&a, &b);
// now, a = 13, b = 7


Setting a pointer variable within a function (e.g. in a "factory" function):
C
void create_string(char **string, size_t size)
{
   *string = (char*)malloc(size);
   ...
}
...
char *s;
create_string(&s, 1000);


Multidimensional array:
C
int arrayNx1[N];         // store N int in consecutive memory
int arrayNxM[N][M];      // store N *int in consecutive memory,
                         // each pointing to individual chunks
                         // of M int in consecutive memory each
int arrayUxVxW[U][V][W]; // arrayUxVxW is an array to U **int
                         // each arrayUxVxW[u] is an array of V *int
                         // each arrayUxVxW[u][v] is an array of W int
etc.
You see here that you have the power to do the things you need (e.g. multi dimensional arrays), but you have the responsibility to handle with care. To initialize such multi dimensional arrays is left as exercise ;-).

Cheers
Andi

PS: Not all variables are stored in memory. A possible optimization is to store variables in CPU registers only. But this is only possible if you do not access the address of the variable explicitly (a CPU register is not located in memory and therefore has no address). This optimization is left to the compiler, though.
 
Share this answer
 
Comments
Maciej Los 14-Dec-14 10:41am    
5ed!
Andreas Gieriet 14-Dec-14 11:06am    
Thanks for your 5!
Cheers
Andi
Member 11287281 5-Mar-15 17:01pm    
Thank you Andreas, this really clears some of my concepts!!
All you need is to know what a pointer is and using your brain. If you know how to point to any object and access it through pointer, you should be able to figure out what to do if that object is itself a pointer, by quite simple reasoning. It will take some minimal practice.

PIEBALDconsult said in his comment: "if you don't know why, stay away from C". Not sure. I would say: if you cannot figure why just using your reasoning skills, stay away from programming.

Please don't get me wrong: I'm not insisting that anyone needs to have C skills. I only say that the pointer operation is some basic thing which all the developers should master. Yes, there are many people formally considered as "developers" without these very basic skills, but who take them seriously? So, this is one of the first pretty basic barriers. You need to overcome it, all by yourself, probably right now. You don't need any manuals or tutorials except a basic reference book on C. This is not as hard as it may seem.

—SA
 
Share this answer
 
Comments
Andreas Gieriet 13-Dec-14 7:54am    
Fully agreed! My 5.
It's crucial to have a basic understanding of what a memory mapped system is, what a memory address is, what a variable in such an architecture is, how to access such a memory location, etc. Without having that basic understanding one will never excel (i.e. will not get beyond mediocrity).
Cheers
Andi
Sergey Alexandrovich Kryukov 13-Dec-14 21:46pm    
Thank you, Andi.
—SA
Maciej Los 14-Dec-14 10:41am    
5ed!
Sergey Alexandrovich Kryukov 14-Dec-14 18:21pm    
Thank you, Maciej.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900