Search code examples
c++dynamic-arrays

creating a dynamic array?


    keyMain= new Key_Node[X];
    for(int i=0; i<X; i++)
    {
        keyMain[i].key=i;
        cout << keyMain[i].key<<endl;
        keyMain[i].next_package=NULL;
    }

Am I doing it right here? I am not sure about it. It seems like the right thing to do, but can anybody confirm? Thank you.X is the number inputted by the user.


Solution

  • Yes, you can certainly do that, provided the array is not expected to grow or shrink.

    Otherwise, std::vector (see here) is probably a better choice. C++ provides some powerful collection classes in its standard library and everyone who professes to know the language should be familiar with them.