Declaration Question

Dave Eisen dkeisen at Gang-of-Four.Stanford.EDU
Tue Jul 25 13:11:25 AEST 1989


In article <1242 at atanasoff.cs.iastate.edu>, hascall at atanasoff (John Hascall) writes:
>In article <394> setzer at nssdcs (William Setzer (IDM)) writes:
>>Will this declaration:
> 
>>char *foo[K][N];
> 
>>  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:


As I read his diagram, foo is a *pointer* to an array K of pointers to array
N of char. So foo is declared:


char  (*(*foo)[K])[N]

and the declaration of foo will only allocate the initial pointer. To
allocate everything you have here, you have to malloc space for an array of
pointers and then do the same for the characters each of these pointers
points to.


Dave Eisen 	 	                                (415)723-2963 "work"
814 University Avenue                   		(415)324-9366  home
Palo Alto, CA 94301                       dkeisen at Gang-of-Four.Stanford.EDU



More information about the Comp.lang.c mailing list