2

I know I should know this, but it's late and my brain just won't put the pieces together.

This is as straight forward as a question can get:

I have a struct item. I want to create a pointer to an array of pointers to that item type.

Eg.

struct item {
    int data;
    string moreData;
};

I want to have an ArrayPointer that point's to an array. I want that array to contain in each element a pointer to an item.

How do I do this in C++, or more sepcifically where do I need to put how many dereferencing operators? I know how to declare basic (single indirection) pointers and am pretty fluent in their use.

I need information for the following steps if at all possible:

Declaring the ArrayPointer.

Initializing the ArrayPointer with a size s.

Initializing each element of ArrayPointer with new item.

eg:

for(int i = 0; i < s; i++)
    ArrayPointer[i] = // a new item

I feel like as soon as someone posts an answer I'm going to facepalm so hard I break my nose.

John Saunders
  • 160,644
  • 26
  • 247
  • 397
user3776749
  • 667
  • 1
  • 10
  • 20

3 Answers3

1

If I have understood correctly then you need something like this

item **ArrayPointer = new item *[s];

for ( int i = 0; i < s; i++ )
{
    ArrayPointer[i] = new item; { i, "More Data" };
}

Or

item **ArrayPointer = new item *[s];

for ( int i = 0; i < s; i++ )
{
    ArrayPointer[i] = new item;
    ArrayPointer[i]->data = i;
    ArrayPointer[i]->moreData = "More Data";
}

To free the allocated memory you can in reverse order

for ( int i = 0; i < s; i++ )
{
    delete ArrayPointer[i];
}

delete [] ArrayPointer;

Otherewise if s is a constant then you may simply declare an array of pointers. For example

item * ArrayPointer[s];
for ( int i = 0; i < s; i++ )
{
    ArrayPointer[i]->data = i;
    ArrayPointer[i]->moreData = "More Data";
}
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • Thank's a lot Vlad. The bit I was missing was `new item *[s]` I've never seen that exact statement before. All of my attempts involved `new *items[s]` I just couldn't seem to find that specific syntax anywhere. Thanks again! – user3776749 Nov 23 '14 at 05:39
  • Sweet. I can also now initiate each element to NULL so I can keep the amount of memory I'm using minimal. – user3776749 Nov 23 '14 at 06:05
0

What you want is an array of struct item *, which are pointers to item structs.

An array of such pointers is a struct item **.

#include <string>
#include <cstdlib>
using namespace std;

struct item {
    int data;
    string moreData;
};

struct item * newItem(int data, string moreData) {
    struct item *result = (struct item *) malloc(sizeof(struct item));
    result->data = data;
    result->moreData = moreData;
    return result;
}

struct item ** array;  // We don't know the size of the array in advance.

int main() {
    int arraySize = 3;  // We get this value from somewhere (user input?).
    array = (struct item **) malloc(3*sizeof(struct item *));
    // Now the array has been allocated. There is space for
    //  arraySize pointers.
    array[0] = newItem(5, "ant");    // Let's make some items. Note that
    array[1] = newItem(90, "bear");  //  newItem() returns a pointer to
    array[2] = newItem(25, "cat");   //  an item.
    return 0;
}
Michael Laszlo
  • 12,009
  • 2
  • 29
  • 47
  • I know how to do that, the problem is that I need the array to be pseudo-dynamic (I need to declare its size once during run-time). The simple version would be: `item *psDynamicArr; psDynamicArr = new item[size];` But in that case the array would contain items, I need it to contain pointers to items. – user3776749 Nov 23 '14 at 05:33
  • Okay, I've modified the code so that you don't declare its size in advance. – Michael Laszlo Nov 23 '14 at 05:35
0

file.h

struct item {
    int data;
    string moreData;
};

item ** array;

file.cpp

array = new item*[s];

for(int i = 0; i < s; i++)
{
    array[i] = new item;
    array[i]->data = 10;
    array[i]->moreData = "data";
}
Ali Mofrad
  • 308
  • 5
  • 21