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

  1. #1
    Registered User
    Join Date
    May 2018

    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
    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
    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. #2
    Registered User
    Join Date
    May 2018
    Sorry the visual didn't come out too good

  3. #3
    Registered User
    Join Date
    Dec 2017
    If you want to point to nodes, then you need node pointers. You also seem to be undersizing your arrays by one.
    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:
    #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';
    Last edited by john.c; 05-17-2018 at 02:46 PM.
    The best argument against democracy is a five minute conversation with the average voter. - Churchill

  4. #4
    Registered User Sir Galahad's Avatar
    Join Date
    Nov 2016
    The Round Table
    Is there a reason why you can't use std::vectors for this?

  5. #5
    Registered User
    Join Date
    May 2018
    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

     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?

    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
    Last edited by DaRealFonz; 05-17-2018 at 07:27 PM.

  6. #6
    Registered User
    Join Date
    May 2018
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 12-20-2011, 09:43 PM
  2. Replies: 4
    Last Post: 07-01-2009, 01:53 PM
  3. Array of pointers to point objects
    By totalfreeloader in forum C++ Programming
    Replies: 6
    Last Post: 11-27-2003, 09:26 AM
  4. Replies: 5
    Last Post: 09-10-2003, 09:30 PM
  5. Replies: 2
    Last Post: 01-29-2003, 03:32 PM

Tags for this Thread