# Dynamic array of matrices

Discussion in 'C Programming' started by Prototipo, Nov 1, 2003.

1. ### PrototipoGuest

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

Prototipo, Nov 1, 2003

2. ### Tom St DenisGuest

"Prototipo" <> wrote in message
news:...
> 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

Tom St Denis, Nov 1, 2003

3. ### Artie GoldGuest

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.

Artie Gold, Nov 1, 2003
4. ### disGuest

"Prototipo" <> wrote in message
news:...

> 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;
}

dis, Nov 1, 2003