Posted by Mark Burford on 2000-04-26
Nested for loop sare the key!, DOuble pointers too.
int* AllocateMatrix(int *sizei, int *sizej)
{
int **Ptr, b;
/*This line reads: cast the memory to be type int, the memory to be
allocated
is decided by the fact that the contents of size (4) multiplied by the
result
of the sizeof function. If the result of all this is NULL, it prints an
error
message.*/
( Ptr = malloc((*sizei) * sizeof(int*)));
for(b-0; b<(*sizei); b++)
Ptr[b] = malloc((*sizej) * sizeof(int));
return(*Ptr);
}
----- Original Message -----
From: <pontingdavid@xxx.xxx>
To: <mrb199@xxx.xxxxx.xx.xx>
Sent: 26 April 2000 11:31
Subject: Re: Dynamic 2d arrays
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> On Wed, 26 Apr 2000, Eian wrote:
> > 2 dimensional array 100x100:
> >
> > int twoD[100][100];
> >
> > 3 dimensional array 20x50x100:
> >
> > int threeD[20][50][100];
> >
> > etc. etc. Very easy really. :) Enjoy!
> >
> > Eian
> >
> Errr.... That's not dynamically allocated. Dynamic allocation is when you
> allocate the appropriate memory for an array based on values learned later
> in the program, e.g.
>
> int *i, a, Depth;
> .
> .
> ~~get Depth from somewhere
> .
> i = (int *)malloc(Depth*sizeof(int));
> for(a=0;a<27;a++){
> i[a]=a;
> }
> for(a=0;a<27;a++){
> printf("%d\n", i[a]);
> }
> free(i);
>
> I can do a 1d array in this way, but it doesn't work for 2d. Thanks again,
>
> -Dave Ponting <pontingdavid@xxx.xxx>
>
> "If ignorance is bliss, why aren't -o)
> there more happy people?" /\
> -Anon _\_v
>
>
> -----BEGIN PGP SIGNATURE-----
> Version: PGP 6.5.1i
> Charset: cp850
>
> iQA/AwUBOQbELmUEHPY27/15EQI1HwCghRPKdRc2pS60p39b2lOu5PIDNBYAn3E0
> OJhTZgmTlaOXZrRgQrMIQdcj
> =/pOR
> -----END PGP SIGNATURE-----
>
>
> >
>
Previous post | Next post | Timeline | Home