setzer@nssdcs (William Setzer (IDM)) (07/25/89)
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 | +-------+ +---...---+ | : | | : | | : | | : | +-------+ +---...---+ | ptrK ------> | N chars | +-------+ +---...---+ (Unfortunately, a dbx on my machine proved inconclusive.) Thanx in advance. -- William Setzer setzer@epsl.umd.edu ; My 'real' mail address.
hascall@atanasoff.cs.iastate.edu (John Hascall) (07/25/89)
In article <394> setzer@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
dkeisen@Gang-of-Four.Stanford.EDU (Dave Eisen) (07/25/89)
In article <1242@atanasoff.cs.iastate.edu>, hascall@atanasoff (John Hascall) writes: >In article <394> setzer@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@Gang-of-Four.Stanford.EDU
setzer@nssdcs.gsfc.nasa.gov (William Setzer (IDM)) (07/26/89)
In article <1242@atanasoff.cs.iastate.edu> hascall@atanasoff.cs.iastate.edu.UUCP (John Hascall) writes: >In article <394> setzer@nssdcs (William Setzer (IDM)) writes: >> [Stuff I said] > "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 I thought that things adjacent to a name had priority over things farther away, ie, '*foo[][]' = '(*(foo[]))[]'. Oh well, I guess I'd better check again. > and you will need to malloc the K sets of "N chars" yourself Then that means that if you say 'char (*foo[K])[N]', then at best the '[N]' part is lint fodder (no offense to lint intended). What's the point? -- William Setzer setzer@epsl.umd.edu ; My 'real' mail address
setzer@nssdcs.gsfc.nasa.gov (William Setzer (IDM)) (07/26/89)
In article <10847@polya.Stanford.EDU> dkeisen@Gang-of-Four.Stanford.EDU (Dave Eisen) writes: >In article <1242@atanasoff.cs.iastate.edu>, hascall@atanasoff (John Hascall) writes: >>In article <394> setzer@nssdcs (William Setzer (IDM)) writes: >>>[Stuff I said] >>[Stuff John Hascall said] >As I read his diagram, foo is a *pointer* to an array K of pointers to array >N of char. Oops! I just wanted to indicate that foo contains the 'left' block of memory. It's not intended to be a pointer to the block. Maybe I should have drawn: foo: +---- ... | ptr ... +- ... -- William Setzer setzer@epsl.umd.edu ; My 'real' mail address.