Newsgroups: comp.lang.c Path: utzoo!utgpu!jarvis.csri.toronto.edu!champlain.dgp!flaps From: flaps@dgp.toronto.edu (Alan J Rosenthal) Subject: Re: vector initialization Message-ID: <1989Jun14.152327.18980@jarvis.csri.toronto.edu> Keywords: vectors, char** Organization: Dynamic Graphics Project, University of Toronto References: <687@lakesys.UUCP> <17940@mimsy.UUCP> <11883@bloom-beacon.MIT.EDU> Distribution: na scs@adam.pika.mit.edu (Steve Summit) writes: > typedef struct { > char *words[MAXWORDS]; > int (*fcn)(); > short flags; > } COM; > > COM foo[] = { > {{ "one", "two", "three", NULL }, do_num, 0,}, > {{ "exit", "quit", NULL }, quit, 0,}, > }; ... >In fact, you can leave out the explicit NULLs as list endmarkers, because >uninitialized char *'s (in this case, the entries out to MAXWORDS) are >guaranteed to be set to null pointers. However, if you leave them out and you happen to have exactly MAXWORDS words in one of the structures, strange things may happen as your program continues to go down the array. Of course, your program could check for the MAXWORDS limit as well as for the null pointer, in which case it would work out fine, but if it doesn't, you should include the extra NULLs because then you would get a compile-time error in the case of having MAXWORDS words (and hence MAXWORDS + 1 initializers). ajr -- "The winners write the history books."