Coder's Guild Mailing List

Re: a dynamic array question

Posted by Maranhao on 1998-03-17

At 04:11 AM 3/17/98 +0100, you wrote:
>
>just a small question (i feel stupid asking). how do you declare a dynamic
>array and why? and how would you put it to use? the reason i ask is because
>where i would have used a linked list, someone else said a dynamic array
>would work.
> Reice
>
>

A dynamic array is used when you need to declare an array after the program
has started. For example if you wanted to know how many elements would be
needed for the array, then you could ask the user then create one
dynamically. Here's how to create a dynamic array of integers in C++.

int *array // Pointer to the first element in the array
int n = 10; // number of elements in the array

array = new int[n];

the pointer array can be accesed just like a normal array even though it's a
pointer, ex. array[1], array[8] ect..

--Dave