Declaration Question

John Hascall hascall at atanasoff.cs.iastate.edu
Tue Jul 25 10:33:25 AEST 1989


In article <394> setzer at nssdcs (William Setzer (IDM)) writes:
>Will this declaration:
 
>char *foo[K][N];
 
>allocate the following picture?  (i.e.  Will it initialize
>the pointers properly and allocate memory for all the blocks shown?)
>If not, is there a (single) declaration that will?
 
>  foo
>   |
>   v
>+-------+      +---...---+
>| ptr1 ------> | N chars |
>+-------+      +---...---+
>| ptr2 ------> | N chars |
>+-------+      +---...---+
>|   :   |      |    :    |

   "char *foo[K][N]" is "array K of array N of pointer to char" which doesn't
   match your picture, how about "char (*foo[K])[N]" which is "array K of
   pointer to array N of char" which seems to match your picture--and you
   will need to malloc the K sets of "N chars" yourself:

   for (i = 0; i < K; i++)
      foo[i] = (char*)malloc(K*sizeof(char));

      /* checking for NULL from malloc left as an exercise */


John Hascall
ISU Comp Center



More information about the Comp.lang.c mailing list