# Thread: Array of pointers to point to another array of pointers

1. ## Array of pointers to point to another array of pointers

Hello, I'm having some difficulties getting an Array of pointers to point to another array of pointers.

for example, I would like to have a "Table" that can point to various other arrays, these arrays, in turn, will point to some nodes.

* I know this is similar to a 2D array but not all the arrays from the table will be the same size.

a visual example:

 Table[0] Table[1] Table[2]
Ary[0] -- nodeA Bry[0] -- nodeG Cry[0] -- nodeK
Ary[1] -- nodeC Bry[1] -- nodeX Cry[1] -- nodeN
Bry[2] -- nodeM Cry[2] -- nodeP
Bry[3] -- nodeZ

The code would be something like
Code:
```struct node{
string name;
int value;
};

int** Table;
int* Ary;
int* Bry;
int* Cry;

//Someplace in a function

void myFunction(){
Table = new int* [2];
Ary = new int[1];
Bry = new int [3];
Cry = new int [2];

Table[0] = Ary;
Table[1] = Bry;
Table[2] = Cry;

Ary[0] = newNode(); //  make a new node function for the struct
Ary[1] = newNode(); // etc....etc.. for Bry and Cry

}```
or would that not work since int* would only be able to take an integer value?

what if I did
Code:
```int** Table;
int** Ary;
int** Bry;
int** Cry;```
Another way of looking at it would be:
Search my Library(Table) for book A(Ary)
in Book A look for page 12(Node A).

Thank you

2. Sorry the visual didn't come out too good

3. If you want to point to nodes, then you need node pointers. You also seem to be undersizing your arrays by one.
Code:
```Table[0]        Table[1]        Table[2]
=============================================
A[0] -- nodeA   B[0] -- nodeG   C[0] -- nodeK
A[1] -- nodeC   B[1] -- nodeX   C[1] -- nodeN
B[2] -- nodeM   C[2] -- nodeP
B[3] -- nodeZ

struct node {
string name;
int value;
};

node *A, *B, *C;
node **Table;

Table = new node*[3];
Table[0] = A = new node[2];
Table[1] = B = new node[4];
Table[2] = C = new node[3];```
And of course, you don't actually need A, B, and C. You could just assign the new memory directly to the Table elements.

EDIT: Another idea:
Code:
```#include <iostream>
using namespace std;

struct Node {
int value;  // got rid of string for simplicity
};

struct NodeArray {
int size;
Node *nodes;
};

void init(NodeArray &na, int sz) {
na.size = sz;
na.nodes = new Node[sz];
for (int i = 0; i < sz; i++)
na.nodes[i].value = i;
}

int main() {
NodeArray *Table = new NodeArray[3];
init(Table[0], 2);
init(Table[1], 4);
init(Table[2], 3);

for (int i = 0; i < 3; i++) {
for (int j = 0; j < Table[i].size; j++)
cout << Table[i].nodes[j].value << ' ';
cout << '\n';
}
}```

4. Is there a reason why you can't use std::vectors for this?

5. john.c
thank you for your reply. Yes, I just realized my arrays were off by one, thank you for pointing that out. Your example appears to do what I want and it looks less cumbersome.

Ok, so what I'm doing is a little side project for personal use. I am making a table to store data about local trees in my area, I have written a small program that asks a few questions to help identify a tree type.

The reason for the Table and array with nodes was bc this is how I wanted to arrange the data

Code:
``` Conifers[0]                                Hardwoods[1]
==============================
Pines[0] -- type1, type2               Magnolia[0]-- type1, type2, type 3
Oaks[1]-- type1, type2, type3, type4```
using your suggestion I can probably do something like this, right?

Code:
```struct Node {
int value;
node* next;  //Just add a pointer
};

NodeArray:         Conifers                                              Hardwoods
=================================
Node[0] -- node-> node ->node           Node[0] -- node-> node
Node[1] -- node-> node-> node```
I don't know if that makes sense, but essentially having the data saved such that I can follow the data like this for example: Hardwoods -> magnolias - eastern magnolia. OR Hardwoods -> Oaks -> water oak

PS..IDK why my table keeps coming out messed up

6. Sir Galahad, no there is no reason and if you have something to share I would gladly take it into consideration. I am just messing around with a little program I was writing for personal use. I came to thinking about linking pointers to other pointers and so on......might not be practical for this particular application but maybe for another project. Just wanted to see how it would be possible to do something like it.