How do I use Malloc to dynamically create a two dimensional array

Kevin Hill kevin at crash.cts.com
Sat Jan 19 08:13:16 AEST 1991


   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
*******************************************************************************



More information about the Comp.lang.c mailing list