[comp.lang.c] How do I use Malloc to dynamically create a two dimensional array

kevin@crash.cts.com (Kevin Hill) (01/19/91)

   I have a problem, and it may seem basic, but how do I use
malloc to create an array of type int i[100][100];

  Should I use a pointer to a pointer like
 int (*i)[100];
 or should I use
 int **i;

  I have been told that the following will work,
 
  int **i;

 for (x = 0; x < 100; x++)
  {
     i[x] = malloc(sizeof(int)*100);
  }


   but it seems to me that this would be a very sloppy way to define the whole
array.  It could end up giving you an array that is all over the place in memory.
  So, If you will please bear with me on this question, thanks,  as the three
or four books that I do have on C, don't seem to go into great detail in
allocating two dimensional arrays dynamically... Thanks..
*******************************************************************************
*  Kevin Hill
*  San Diego State University, Accounting Major.
*  No matter where you go, there you are.
*******************************************************************************
*               \
*         *=====+)---------------------------
*               / 
*  San Diego State Fencing
*******************************************************************************

gelinasm@merrimack.edu (Mark Gelinas) (01/19/91)

In article <7046@crash.cts.com>, kevin@crash.cts.com (Kevin Hill) writes:
>    I have a problem, and it may seem basic, but how do I use
> malloc to create an array of type int i[100][100];
>

	Since 2-D arrays can exist as consecutive addresses in memory,
(stored/read rowwise I believe), why not try something like

	int **i;
	int rowsize, colsize;
	.
	.
	*i = (int *) malloc(sizeof(int)*rowsize*colsize);

For example, a 100 x 100 array would be

	int **i;
	int rowsize=100, colsize=100;
	.
	.
	*i = (int *) malloc(sizeof(int)*rowsize*colsize);

I have not tried this myself, but from what I can recall, it should work.

Corrections, questions, additional suggestions welcomed.

Mark

P.S.  Anyone know of any graduate schools with chemistry programs
      geared toward computer applications (data modelling, computer
      to instrument interfaces, simulations, etc)?  If so, please
      mail me direct.  Thanks.

-- 
- - - - - - - - - + - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Mark Gelinas      | UUCP:    samsung!hubdub!GELINASM
Merrimack College | or       GELINASM@merrimack.edu

davel@cai.uucp (David W. Lauderback) (01/20/91)

In article <20981.27984433@merrimack.edu> gelinasm@merrimack.edu (Mark Gelinas) writes:
>In article <7046@crash.cts.com>, kevin@crash.cts.com (Kevin Hill) writes:
>>    I have a problem, and it may seem basic, but how do I use
>> malloc to create an array of type int i[100][100];
>>
>
>	Since 2-D arrays can exist as consecutive addresses in memory,
>(stored/read rowwise I believe), why not try something like
>
>	int **i;
>	int rowsize, colsize;
>	.
>	.
>	*i = (int *) malloc(sizeof(int)*rowsize*colsize);
>
>
>I have not tried this myself, but from what I can recall, it should work.
I have used this method.
>
>Corrections, questions, additional suggestions welcomed.
>
>Mark
>

While that will work on most (if not all compilers), I believe the "correct"
way to do this is as follows:

func()
{
	extern void	*malloc();   /* this sould be in a header not here */
	int	(*malloced)[100];    /* parentheses are important */
	/* you want a pointer to an array of a 100 int's */
	/* not a 100 int pointers */

	malloced = (int (*)[100]) malloc(sizeof(int [100][100]));
}
While the (type def) looks awful, what's (*).  If you remember this simple
rule, you will never have problems with type casts.  Take the variable's
declaration "int (*malloced)[100]", remove the variables name and surround
it with parentheses and you've got the typedef you want "(int (*)[100])".
-- 
David W. Lauderback (a.k.a. uunet!cai!davel)
Century Analysis Incorporated
Disclaimer: Any relationship between my opinions and my employer's
	    opinions is purely accidental.