Click here to Skip to main content
15,886,110 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Assuming that there is a functional program built around this will the following work:

C++
Item** items;
Item* item;
int nItems;

items[nItems] = item;


Or would I have to set each member variable of each instance of Item more like this:

C++
int newId = item->getId();


items[i]->setID(newId);
Posted
Comments
Matt T Heffron 9-Oct-15 19:40pm    
Well items is: pointer to (an array of) pointer(s) to an Item, but items is never initialized to point to the array so, first of all, items needs to be set to point at the (newly allocated) array.

1 solution

The first option will work with some modifications, provided that the variables are initialized properly.

C++
int nItems = 5;
Item** items = (Item**)calloc(nItems, sizeof(Item*));
Item* item = new Item(); // If Item is a class

items[nItems] = item;  // nItems will be outside the length of the array
items[0] = item;



// Cleanup
if (items != null)
{
    for (int i=0; i<nitems;>    {
        if (items[i] != null)
            delete items[i];
    }
    free(items);
    items = null;
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900