1

I'll cut to the chase to save you all some boredom of surplus reading:

I've tried to comb the internet in search of tutorials over dynamical arrays of objects/classes where they explain how pointers are implemented here. This in particular: TheClass **foo[10]; or something like that, I don't understand what two stars/asterisks are good for and how they are used. And this whole thing in general.

I do know how dynamical arrays are declared,how regular pointers are used,how to make classes,how to make dynamic arrays of classes. But all this combined got me confused.

So my questions are:

  1. What does this do and how does it work?

  2. Can you recommend a site where you know examples/tutorials of this can be found?

  3. Does this have a specific name rather than "dynamic object arrays with double pointers" or whatnot?

  4. If no tutorial comes to mind, I'd appreciate it if you could make a very, very brief example.

Like for instance

int *something;
int somethingElse = 10;
something = &somethingElse; /*Now when you change "somethingElse","something" 
                              will also change to the same number*/

A little super-short example and explanation like that would be greatly appreciated. =)

Chait
  • 1,052
  • 2
  • 18
  • 30
user1770094
  • 87
  • 1
  • 3
  • 8
  • 3
    So ... you don't understand pointers to pointers? Is that it? – Useless Jan 11 '13 at 23:27
  • If that is what it is called,then yes. – user1770094 Jan 11 '13 at 23:32
  • `X *x` is a pointer. `X **x` is a pointer to a pointer. `X **x[10]` is an array of 10 pointers to pointers. Now you know, and if not, you at least know what to search for. – Useless Jan 11 '13 at 23:38
  • @user1770094: Grab one of our [recommended beginner C++ books](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) and get reading. In the meantime (and forever after that) use `std::vector<>` for your dynamic array needs. – GManNickG Jan 11 '13 at 23:38
  • I'm already googling up any tutorials related to pointers to pointers,many thanks. =) – user1770094 Jan 11 '13 at 23:42
  • 1
    If you *understand* pointers then pointers to pointers aren't really complicated. – johannes Jan 11 '13 at 23:43

4 Answers4

2

The simplest, more or less usefull, example using pointers to pointers would be a two dimensional array. So for example

//Create a pointer to (an array of) pointers to (an array of) ints
int** array2D;

//Create a array of pointers to (an array of) ints and assign it to the pointer
array2D = new int*[10];

//Assign each of the pointers to a new array of 10 ints
for(size_t i =0; i<10;i++) {
    array2D[i] = new int[10];
}

//Now you have an 2 dimensional array of ints that can be access by
array2D[1][3] = 15;
int test = array2D[1][3];

I hope this explains a bit what pointers to pointers are and how they work.

Haatschii
  • 9,021
  • 10
  • 58
  • 95
  • But this question has an array of double pointers (one more level of indirection). – Ben Voigt Jan 11 '13 at 23:43
  • @BenVoigt: True, but I hope that once one understands the concept of pointer to pointer to X the next stept should be easy. Also I don't have a good example for that level in my mind. ;) – Haatschii Jan 11 '13 at 23:49
1

Well i see you aim for the complete answer, i'll give u a brief example on that.

If you define an array of pointers to pointers, like in your "class foo**[10]" example, let's say:

int numX = 100;
int numY = 1000;

Node **some[10];
    some[0] = new Node*[numX];
    some[0][0] = new Node[numY];

Then what it does mean is:

You have 10 Node** in your 1st level. So you have 10 pointers to type Node**, but they aren't pointing anywhere useful yet.

This is just 10 adjacent memory locations for storing pointers. In this case it is irrelevant what they are pointing to, mainly it is just 10 fields containing space for a pointer.

Then take the first of these 10 "spaces" and assign the address of an array of 100 pointers to type Node*

some[0] = new Node*[numX]; //numX = 100

This is done and evaluated during runtime, so u can use a variable value given by user input or some application logic, to define dimensions of arrays aka memory-fields.

Now you have 1 of 10 pointers pointing to 100 pointers to type Node*, but still pointing to a black hole.

In the last step create 1000 objects of type Node and attach their addresses to the first of your 100 pointers.

some[0][0] = new  Node[numY]; //numY = 1000

In the above example this means, only [0][0][0] to [0][0][999] are objects, the 1000 you created with:

This way you can build multi-dimensional arrays with the specified type. What makes the whole thing work, is that you instantiate what you need in the last dimension (3) and create pointers to uniquely index every field created from [0][0][0] to [9][99][999].

some[0][1]; // memory violation not defined
some[0][0]; // good -> points to a *Node 
some[0][0][0]; // good -> actually points to Node (data)
some[1][0][0]; // memory violation not defined 

As far as i know, most often you use a one-dimensional array and some tiny math, to simulate two-dimensional arrays. Like saying element [x][y] = [x+y*width];

However you want to use your memory, in the end it all boils down to some memory-adresses and their content.

Lemonade
  • 503
  • 2
  • 8
1

A pointer is, well, a pointer. It points at something. Period. If you understand that much, then you should be able to understand what a pointer to a pointer is. It is just a pointer whose value is the memory address of another variable that is itself a pointer to something else. That's all. Every time you add a * to the mix, it is just another level of pointer indirection. For example:

int i;
int* p_i = &i; // p_i is a pointer to an int and points at i
int** pp_i = &p_i; // pp_i is a pointer to an int* and points at p_i
int*** ppp_i = &pp_i; // ppp_i is a pointer to an int** and points at pp_i

Now apply that concept to TheClass **foo[10]; (which is actually TheClass** foo[10]; from the compiler's perspective). It is declaring an array named foo that contains 10 TheClass** pointer-to-pointer elements. Each TheClass* might be a pointer to a specific TheClass object, or it might be a dynamic array of TheClass elements (no way to know without more context), then each TheClass** is a pointer to a particular TheClass* pointer.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • That summarized it extremely well.I got the hang of it now.But now I'm wondering why anybody would use more pointers.What kind of problem does this solve/what is this for usually? – user1770094 Jan 13 '13 at 09:30
  • Like I said, it is hard to say for sure without knowing the context in which it is being used. It is common to use pointer-to-pointer, such as creating a dynamic array of object pointers. But an array of pointer-to-pointer is not as commonly used. One area that it could be used for is a lookup table. In this case, `foo` could be an array of 10 items that initially contain pointers to pointers to stubs, then the pointer-to-stub values could be swapped out at a later time during the app's lifetime without changing the contents of the array itself. – Remy Lebeau Jan 14 '13 at 19:40
0
TheClass** foo[10];

This line of code tells the compiler to a make an array called foo of 10 elements of type pointer to (pointer to Theclass ).

In general when you want to figure out a type that involve multiple astrisks, ampersands. Read Left to Right. so we can break out the code above to something like this:

   (  (Theclass)  *  )  *  foo[10] 
          ^       ^     ^    ^   ^
          5       4     3    2   1

#1 an array of 10 elements named #2 foo #3 of type pointer #4 to pointer #5 to Theclass 
AlexDan
  • 3,203
  • 7
  • 30
  • 46