vector initialization

Chad Gibbons chad at lakesys.UUCP
Thu Jun 8 23:14:06 AEST 1989


In article <11883 at bloom-beacon.MIT.EDU> scs at adam.pika.mit.edu (Steve Summit)
writes:

|The other way is to modify the declaration, so that you don't
|need a pointer:
|	typedef struct {
|		char *words[MAXWORDS];
|		int (*fcn)();
|		short flags;
|	} COM;
|	COM foo[] = {
|		{{ "one", "two", "three", NULL }, do_num, 0,},
|		{{ "exit", "quit", NULL }, quit, 0,},
|	};
|This has the advantage that you don't need to think of names for
|the placeholder pointers.  It has the disadvantages that the
|number of entries is limited, and space is wasted if there are
|many lists with considerably fewer than MAXWORDS entries.

	Of course this disadvantage (wasted space) takes away from the
reason of using the char ** type to begin with.  A single entry might
have as few as one entry and has many as _fifty_--lots and lots of
wasted space there, unfortunately.  It would be quite nice to be able to 
specify the list the same way one does when initializing an array of
pointers, but the world doesn't always work the way you want it to.

|The method using dummy arrays, which Chris illustrated, is
|generally preferable, especially if you don't mind making up
|names.

	Yes; the extra names don't make a difference to me--they can be
declared static in a given module and then you wouldn't have to worry
about someone "forgetting" what the name you used was and wind up using
it for something else, not that this would matter or anything. 

	 Of course, once you find out your compiler only supports 4K of
literal strings, and you need at least 16K worth, this doesn't matter
any more. That is somewhat of a good thing; imagine trying to do a 
linear-type search through a 16K list _all the time_.  If I have to store
them external to the code, at least I can throw them into some type of
structure where searching goes quickly.
-- 
D. Chadwick Gibbons, chad at lakesys.lakesys.com, ...!uunet!marque!lakesys!chad



More information about the Comp.lang.c mailing list