1

I am trying to write a pointer array to structs in C++. My main goal is to be able to dynamically add pointers to the array. I am having trouble with the synthax

  struct items
  {
    int member1;
    int member2;
  };

  int N=5;
  items **ptr=new item *[N]; // This is a ptr to an array of ptr's. Each ptr 
                             // in this array needs to point to an items struct. 

My question is how to write in the struct's objects from this point on. I know I need to create them first but I don't have any idea how to do that.

1
  • 3
    The ideal solution is probably a std::vector<item> or std::vector<std::unique_ptr<item>>. Commented Oct 15, 2013 at 17:13

3 Answers 3

3

You're allocating only an array of pointers of item *, you'll need to allocate the memory for each item also, e.g.:

struct item // naming it 'items' might be confusing, and was used inconsistently
            // in your code sample
{
    int member1;
    int member2;
};

int N=5;
item **ptr=new item *[N];

for(int i = 0; i < N; ++i)
{
    ptr[i] = new item();
}

Accessing your structure members looks like this:

ptr[2]->member1 = 42; // Sets member1 of the 3rd item element to 42

Note that you'll need to free the allocated memory somewhere as follows:

for(int i = 0; i < N; ++i)
{
    delete ptr[i];
}
delete [] ptr;

I general for you'd be better off using a c++ standard container like:

#include <vector>

int N = 5;
std::vector<item> myItems;
myItems.resize(N,item());

myItems[2].member1 = 42; // Sets member1 of the 3rd item element to 42

which would do all the memory management for you internally.
If you're using and do not need dynamically sized arrays you can even avoid heap allocated memory at all using std::array:

#include <array>

std::array<item,5> myItems; // Size is fixed to 5, you can't use a variable here

myItems[2].member1 = 42; // Sets member1 of the 3rd item element to 42
Sign up to request clarification or add additional context in comments.

4 Comments

It's working. What is the advantage in using vectors instead of what I did? I am new to C++ and have not studied about vectors yet.
And also, you wrote that accessing structure members is done with:ptr[2]->member1 = 42;. What is its equivalent? for ex: *(ptr[2]).id?
Don't know what exactly you mean with 'What is its equivalent', but I'd say *(ptr[2]).member1 (there's no field id introduced anywhere). What concerns the advantages of using standard containers, it's clearly written in the answer.
Thank you for the response! As for the field "id" I meant member1. sorry for the confusion.
1

You can add objects to your array by ptr[i] = new items(). And you can access data in items** ptr by ptr[i]->member1. But I'll strongly recommend using stl containers and smart pointers.

Comments

0

Firstly you have to finish the allocation step.

for(int i=0;i<N;i++)
  ptr[i]=new items;

Then you can access the array using e.g.:

ptr[0]->member1=123;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.