dell@amelia.nas.nasa.gov (Thomas E. Dell) (08/24/89)
- Question about Passing Structures and Pointer Initialization - This is actually an interesting problem. I have a structure "OPTIONS" defined as a structure consisting of integers, char *, and longs. It is for a configuration file, but the application is not too important. I pass a number of these structures to a routine to set various items depending on context, as setitem (line, Deflist); as I read through the file. Outlined, the routine looks something like : setitem (string, Forum) char *string; OPTIONS *List; { char *option, *arg; [ set "option" to point to name of configuration item ] [ set "arg" to point to its argument, or to "" if no argument ] if (!stricmp (option, "alias")) /* string arg */ List->alias = strdup (arg); else if (!stricmp (option, "group")) /* integer arg */ List->group = atoi (arg); ...... } etc.. YUCK (There are over 30 available options). My question is I have been unable to create a "configuration array" to associate the string labels "alias", "group", etc. to elements of the structure. Something like: #ifdef UNDEFINED { "alias", CHAR, List->alias }, { "group", INT, List->group }, #endif Though, elsewhere in this program I have a similar setup where I can { "alias", CHAR, NULL, xAlias }, { "group", INT, &xGroup, NULL }, ^ Ints ^ Char *'s for a different configuration, wherein it is possible to initialize the configuration structure to tangible locations. (Yuck, but not as bad as the YUCK above). This program must be "Extremely_Portable" and as such I cannot use Unions, as one of machines does not support them. Can someone who has done something like this before let me know a better way? I hate putting YUCKs in the program. -- Tom dell@ames-nas.arpa ----- (additional background if needed) Structure format. OPTIONS is a linked list structure, hence the funny definition. What is necessary is something like TABLE that I can initialize. I don't mind using really strange things as long as they are fairly portable. typedef struct OPT typedef struct { { char *alias; char *label; int group; int type; ..etc... int *number; } char *string; OPTIONS; } TABLE;