vector initialization

Alan J Rosenthal flaps at dgp.toronto.edu
Mon Feb 1 19:28:53 AEST 2016


scs at 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."



More information about the Comp.lang.c mailing list