Dynamic array of matrices

P

Prototipo

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
 
T

Tom St Denis

Prototipo said:
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
 
A

Artie Gold

Prototipo said:
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
 
D

dis

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top