how to allocate memory dyamically for multidimensional array

B

bijax

hi,
i am new to multidimensional array of c programming .pls help me
to solve out this

how to allocate memory for mutidimensional array?
i.e a[2][4] [5][5];
 
R

Richard Heathfield

bijax said:
hi,
i am new to multidimensional array of c programming .pls help me
to solve out this

how to allocate memory for mutidimensional array?
i.e a[2][4] [5][5];

This is a Frequently Asked Question. The comp.lang.c newsgroup has an
associated FAQ list, which you should have no trouble in finding, and which
will provide you with the answer you seek.
 
D

Daniel Rudy

At about the time of 9/18/2005 11:24 PM, bijax stated the following:
hi,
i am new to multidimensional array of c programming .pls help me
to solve out this

how to allocate memory for mutidimensional array?
i.e a[2][4] [5][5];

int main(void)
{
int a[2][4][5][5];

/* insert program logic here */


/* return to operating system */
return(0);
}

Storage space is allocated when it's declared. Be carefull of int,
depending on your machine, it can be either 16 or 32 bits wide.
 
R

Richard Heathfield

Daniel Rudy said:
At about the time of 9/18/2005 11:24 PM, bijax stated the following:
hi,
i am new to multidimensional array of c programming .pls help me
to solve out this

how to allocate memory for mutidimensional array?
i.e a[2][4] [5][5];

int main(void)
{
int a[2][4][5][5];

You missed the subject line, in which he says "dynamically".
/* insert program logic here */


/* return to operating system */
return(0);
}

Storage space is allocated when it's declared. Be carefull of int,
depending on your machine, it can be either 16 or 32 bits wide.

Or 17, or 19, or 31, or 37, or 41, or 43, or 47, or 53, or 59, or 61, or 67,
or any of a whole bunch of other integers greater than 15. I'd count them
all for you, only I'm running a little late.
 
Z

Zoran Cutura

bijax said:
hi,
i am new to multidimensional array of c programming .pls help me
to solve out this

how to allocate memory for mutidimensional array?
i.e a[2][4] [5][5];

Many different ways!

Say you want an two-dim-array of 5x5 ints. You can allocate

int height = 5, width = 5;
int *a = malloc(height * width * sizeof *a);

and use it like:

a[2*width + 3] which would be a[2][3]

or you can

int **a = malloc(height * sizeof *a);
int i;
for(i=0; i < height; i++) {
a = malloc(width * sizeof *a);
}

which allows you to use
a[2][3];

There are other sollutions, choose whatever fits best to the problem.

Note: You should check the return value of malloc in any case.
 
M

Michel Rouzic

bijax said:
hi,
i am new to multidimensional array of c programming .pls help me
to solve out this

how to allocate memory for mutidimensional array?
i.e a[2][4] [5][5];

well, do this (let's say it's a multidimensionnal array of doubles):

double ****a;
int_8t ia, ib, ic;

a=malloc (2 * sizeof(double ***)); //you can replace 2 by a variable
of course
for (ia=0; ia<2; i++) //and here by the same variable
a[ia]=malloc (4 * sizeof(double **));
for (ib=0; ib<4; i++)
a[ia][ib]=malloc (5 * sizeof(double *));
for (ic=0; ic<5; ic++)
a[ia][ib][ic]=malloc (5* sizeof(double));

Correct me if I'm wrong but I don't think I made any mistake...
 
D

Daniel Rudy

At about the time of 9/18/2005 11:51 PM, Richard Heathfield stated the
following:
Daniel Rudy said:

At about the time of 9/18/2005 11:24 PM, bijax stated the following:

hi,
i am new to multidimensional array of c programming .pls help me
to solve out this

how to allocate memory for mutidimensional array?
i.e a[2][4] [5][5];

int main(void)
{
int a[2][4][5][5];


You missed the subject line, in which he says "dynamically".

I did miss that. In that case...

int *data;
....
....
....
data = malloc(2 * 4 * 5 * 5);
if (data = NULL) exit(-1); /* malloc failed if true */
....
....
(*data)[x * y * x + i] = value;

Or something like that...
Or 17, or 19, or 31, or 37, or 41, or 43, or 47, or 53, or 59, or 61, or 67,
or any of a whole bunch of other integers greater than 15. I'd count them
all for you, only I'm running a little late.

Now why would int have those weird sizes? What hardware out there uses
a 53 bits as it's machine word?
 

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

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,140
Latest member
SweetcalmCBDreview
Top