Re: arrays

From: Jason Wilkins (fenix@IO.COM)
Date: 10/07/98


On Wed, 7 Oct 1998, Fafhrd wrote:

> Is there a way to dynamically create an array of integers? Like I can malloc
> what I need for a struct on the fly, but I need to do so for a simple array.
> eg, I have array[10][10][10] which consumes lots of ram and may not be
> needed. I might need 10, I might need 5.
>
> This is probably such a simple thing, Ill be embarrassed, but for the life
> of me I can't find the answer...
>

This is beginning to feel like beginners C/C++ list ^_^

If I want to an array of ints, 10 x 10 x 10, then I do this

int *array = (int *)malloc(sizeof(int) * 10 * 10 * 10);

if I want to parameterize it, I just do this:

int  num_of_ints;
int *array;

num_of_ints = /*however many I want*/;

.
.
.

array = (int *)malloc(sizeof(int) * num_of_ints);

.
.
.

free(array);
array = NULL;

.
.
.

easy huh?

> Erik Madison
> ICQ #13940294
> fafhrd@rotd.com
>
>
>      +------------------------------------------------------------+
>      | Ensure that you have read the CircleMUD Mailing List FAQ:  |
>      | http://democracy.queensu.ca/~fletcher/Circle/list-faq.html |
>      +------------------------------------------------------------+
>

--
         The Phoenix - President of The Artistic Intuition Company
    Caelius * Zen-X * Mirror Reflex * Runica * X-Domain * Infinite Realms
                          http://www.io.com/~fenix


     +------------------------------------------------------------+
     | Ensure that you have read the CircleMUD Mailing List FAQ:  |
     | http://democracy.queensu.ca/~fletcher/Circle/list-faq.html |
     +------------------------------------------------------------+



This archive was generated by hypermail 2b30 : 12/15/00 PST