Operating System - HP-UX
1830241 Members
1709 Online
109999 Solutions
New Discussion

how to use malloc() to create a multidimension array

 
SOLVED
Go to solution
Xiaoming Zhang
Contributor

how to use malloc() to create a multidimension array

I want to dynamicly create a multidimensional array of a struct by using heap memory function malloc(). How can I do it?

Thanks.

Xiaoming
2 REPLIES 2
Jason VanDerMark
Trusted Contributor
Solution

Re: how to use malloc() to create a multidimension array

I happened to have a link to just the information you are looking for. You should be able to find the answer at the following url:

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

Hope that this helps.

Good Luck,
Jason V.
Tie two birds together, eventhough they have four wings, they cannot fly.
Ulrich Deiters
Frequent Advisor

Re: how to use malloc() to create a multidimension array

The advice on www.eskimo.com is excellent. But a word of caution is in order: In a static array
double a[NROWS][NCOLS];
"a" is a pointer to the first element. In
"self-made" dynamic arrays this is
not the case. A subroutine taking a
static 2D-array as an argument may
not work with dynamic arrays.