2
Contributors
1
Reply
8
Views
3 Years
Discussion Span
Last Post by deceptikon
1

how can I use it ??

A "dynamic array" is really nothing more than simulating an array. Once created, you use it very much like a regular array.

how can I allocate and intialize it ???

This will allocate a dynamic array of 10 pointers to int, then initialize the pointers with an increasing value. The values are printed by iterating over the array and dereferencing each pointer. Finally, the dynamic array is freed:

// Allocate
int **a = new int*[10];

// Initialize
for (int i = 0; i < 10; i++)
{
    a[i] = new int(i);
}

// Use like an array
for (int i = 0; i < 10; i++)
{
    cout << *a[i] << '\n';
}

// Release
delete[] a;
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.