Velocity Reviews > Dynamic array of matrices

# Dynamic array of matrices

Prototipo
Guest
Posts: n/a

 11-01-2003
Hi! I need to dynamically create X vectors of matrices (with a known
size of 5x5). It's like a three-dimensional matrix with 2 known
dimensions and the third unknown.

I have something like:

int *matrix[5][5];

But I don't know how to allocate memory for X matrices. How can I do
it?

thanks

Tom St Denis
Guest
Posts: n/a

 11-01-2003

"Prototipo" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
> Hi! I need to dynamically create X vectors of matrices (with a known
> size of 5x5). It's like a three-dimensional matrix with 2 known
> dimensions and the third unknown.
>
> I have something like:
>
> int *matrix[5][5];
>
> But I don't know how to allocate memory for X matrices. How can I do
> it?

The same way you do with all other matrices

matrix = calloc(X, sizeof(matrix[0]));

Tom

Artie Gold
Guest
Posts: n/a

 11-01-2003
Prototipo wrote:
> Hi! I need to dynamically create X vectors of matrices (with a known
> size of 5x5). It's like a three-dimensional matrix with 2 known
> dimensions and the third unknown.
>
> I have something like:
>
> int *matrix[5][5];
>
> But I don't know how to allocate memory for X matrices. How can I do
> it?
>

See:

http://www.eskimo.com/~scs/C-faq/q6.16.html

[You *did* read the various FAQs *first*, didn't you? ]

HTH,
--ag
--
Artie Gold -- Austin, Texas
Oh, for the good old days of regular old SPAM.

dis
Guest
Posts: n/a

 11-01-2003
"Prototipo" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...

> Hi! I need to dynamically create X vectors of matrices (with a known
> size of 5x5). It's like a three-dimensional matrix with 2 known
> dimensions and the third unknown.
>
> I have something like:
>
> int *matrix[5][5];
>
> But I don't know how to allocate memory for X matrices. How can I do
> it?

#include <stdlib.h>
int main(void)
{
int X = 5;
int (*matrix)[5][5] = malloc(X * sizeof *matrix);
if (matrix != NULL)
{
/* allocation succeeded */
}
free(matrix);
return 0;
}