5

Possible Duplicate:
Uses for multiple levels of pointer dereferences?

I was reading another post and this led me to this question. What the heck would something like this mean? Also how deep do people go with a pointer to a pointer to a pointer to a pointer.... I understand Pointer to Pointer but why else you would go more after that? how deep have you gone in using ****?

Foo(SomePtr*** hello);

Community
  • 1
  • 1
RoR
  • 15,934
  • 22
  • 71
  • 92
  • Many duplicates on SO already, e.g. [Uses for multiple levels of pointer dereferences?](http://stackoverflow.com/questions/758673/uses-for-multiple-levels-of-pointer-dereferences) – Paul R Jan 11 '11 at 09:31

6 Answers6

10

You could refer to a 3 dimensional array of ints as int *** intArray;

Sam Dufel
  • 17,560
  • 3
  • 48
  • 51
  • Hi! Could you please elaborate on what you mean by this?? This is the piece of code I have and I don't understand what's happening: `int ***grid;` `grid = calloc(nx,sizeof(int**));` for (i = 0; i < nx; ++i) { grid[i] = calloc(ny,sizeof(int*)); for (j = 0; j < ny; ++j) { grid[i][j] = calloc(nz,sizeof(int)); } } – secluded Jul 13 '16 at 07:12
  • Its allocating a 3d array of ints with dimensions nx, ny, nz. – Sam Dufel Jul 13 '16 at 15:50
9

It is rare in C++ certainly.

In C it may well show up where:

  • You use "objects" which are structs, and you always pass them around or create them on the heap as pointers.
  • You have collections of such pointers as dynamically allocated arrays thus T** where T is the type.
  • You want to get an array so you pass in a T*** and it populates your pointer with a T** (array of T* pointers).

This would be valid C but in C++:

  • The first step would be the same. You still allocate the objects on the heap and have pointers to them.
  • The second part would vary as you would use vector and not arrays.
  • The 3rd part would vary as you would use a reference not a pointer. Thus you would get the vector by passing in vector<T*>& (or vector<shared_ptr<T> >& not T***
Delimitry
  • 2,987
  • 4
  • 30
  • 39
CashCow
  • 30,981
  • 5
  • 61
  • 92
4

Well, if your function needs to modify a pointer to a pointer ... ;)

See http://c2.com/cgi/wiki?ThreeStarProgrammer for a discussion.

etarion
  • 16,935
  • 4
  • 43
  • 66
2

Pointer to a dynamic array of pointers maybe? Make sense to me.

tdammers
  • 20,353
  • 1
  • 39
  • 56
0

I never go beyond 'pointer to pointer' - I think if you have to then something is fundamentally wrong with your design.

trojanfoe
  • 120,358
  • 21
  • 212
  • 242
0

I think the root cause for this is thinking that "All the problems in the computer science can be solved by one more level of indirection." http://www.c2.com/cgi/wiki?OneMoreLevelOfIndirection

Added level of indirection are often necessary for invoking sub classes methods or directly accessing sub classes elements. And there is no such limit on having pointer to pointer to ...pointer to stuff.