Re: arrays, malloc
From: Barry Schwarz (schwarzb_at_deloz.net)
Date: 03/06/04
- Next message: Doug Harrison [MVP]: "Re: arrays, malloc"
- Previous message: Kyle: "Re: arrays, malloc"
- In reply to: Kyle: "arrays, malloc"
- Messages sorted by: [ date ] [ thread ]
Date: 6 Mar 2004 23:08:55 GMT
On Sat, 6 Mar 2004 20:59:34 +0100, "Kyle" <a@b.c> wrote:
>what's the easy way of malloc'ing 2 dimensional array
>
>i mean i may always do
>
>#define GET(array,i,j,size) array[i+size*j]
>
>and use 1 dim array, but if it's (mallocing 2 dim) not too complicated i
>will be very happy <:
>
The traditional method of allocating a 2D array of type T when the
size of each dimension (number of rows and columns) is not known until
run time is
T **array = malloc(rows * sizeof *array);
for (i = 0; i < rows; i++)
array[i] = malloc(columns * sizeof *array[i]);
You can then refer to elements of the array as array[j][k] anywhere a
variable of type T could be used.
Naturally, all malloc calls should be checked for success.
<<Remove the del for email>>
- Next message: Doug Harrison [MVP]: "Re: arrays, malloc"
- Previous message: Kyle: "Re: arrays, malloc"
- In reply to: Kyle: "arrays, malloc"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|