# any use for 4x pointer or higher?

• 11-20-2003
skorman00
any use for 4x pointer or higher?
out of curiosity, have you found one?
• 11-20-2003
XSquared
By 4x pointer, do you mean something like int ****x? If that's what you mean, you should never run into anything more than two in good code.
• 11-20-2003
Cat
I've seen some pointers like that in tensor mathematics. For example, in mechanics, a stiffness tensor is 3x3x3x3, so often you want to have four subscripts. Although I personally would not use a 4d array, but rather a 1D array and use operator()(int,int,int,int) to index into it.
• 11-21-2003
skorman00
I know its horroble programming style, but I just recently found out that you are able to have up to a 13x pointer (int *************x). I'm just asking around to see if anybody has ever found it practical to take advantage of this, because it is pretty useless.
• 11-21-2003
Prelude
>I'm just asking around to see if anybody has ever found it practical to take advantage of this, because it is pretty useless.
I've found use of up to four levels. I can't think of a situation for many more than that though.

>you should never run into anything more than two in good code.
I like to believe that I write "good code", yet I've used triple pointers quite a few times in non-trivial data structure code. :rolleyes:
• 11-21-2003
XSquared
I can't see any situations where two or less pointers couldn't accomplish the same thing. If you can, please show me.
• 11-21-2003
Prelude
>I can't see any situations where two or less pointers couldn't accomplish the same thing.
Let's say that I need to write a function to dynamically allocate a low level matrix of T. Both x and y need to be arbitrary, but the function is required to return a status instead of the pointer itself to meet consistency requirements for the project. In other words:
Code:

```if ( matalloc ( &matrix, x, y ) != GOOD ) {   // Enter error handling code }```
Now I have no choice but to perform the allocation on my original pointer. The most natural and clear solution is to pass a pointer to the pointer:
Code:

```error_code_t matalloc ( T ***matrix, int x, int y ) {   // Allocate memory and return status }```
With no effort at all I managed to reach three levels of indirection through necessity. Of course, I could use a reference:
Code:

```error_code_t matalloc ( T **&matrix, int x, int y ) {   // Allocate memory and return status }```
But tell me honestly whether you knew what the syntax was for that without seeing it or testing it. If you did, can you be sure that everyone else will too? *** is very obvious to anyone who understands pointers, but reference syntax can be tricky. The natural reaction is to do this:
Code:

`error_code_t matalloc ( T& **matrix, int x, int y )`
You could also wrap the matrix in a structure to be more obvious:
Code:

```struct matrix_s {   int **matrix; };```
But we still need to use a pointer or a reference on the structure so you gain only extra complexity with the use of an unnecessary struct and member accesses to it. Some might argue that this has the benefit of clarity since instead of *** all we use is *, but others might respond that it's overkill. You could even use a global variable, but that would be a bad idea in general.

So, while you can think of ways around more than two levels of indirection (I can as well), that isn't the issue. Sometimes we don't have the luxury of doing anything we want due to restrictions of the coding style or project requirements. I don't think that using the best solution within the restrictions given is bad style. If you think otherwise you should consider whether you are being naive.
• 11-21-2003
Cat
Quote:

Originally posted by XSquared
I can't see any situations where two or less pointers couldn't accomplish the same thing. If you can, please show me.
You can ALWAYS use only two or less pointers, but it may not always be the most intuitive. As I said, I've seen tensors with lots of pointers, although I dunno if I like it much like that.
• 11-22-2003
CornedBee
I think tensors and matrices and all such data structures should be wrapped in classes anyway.