#include #include #include #include "fields.h" typedef struct { char type; int i; float f; char *s; } Item; main() { Item array[5]; int i; IS is; is = new_inputstruct(NULL); /* Read in the items -- if "int", read it into array[i].i if "float", read it into array[i].f if "string", read it into array[i].s */ for (i = 0; i < 5; i++) { if (get_line(is) != 2) exit(1); if (strcmp(is->fields[0], "int") == 0) { array[i].type = 'i'; if (sscanf(is->fields[1], "%d", &(array[i].i)) != 1) exit(1); } else if (strcmp(is->fields[0], "float") == 0) { array[i].type = 'f'; if (sscanf(is->fields[1], "%f", &(array[i].f)) != 1) exit(1); } else if (strcmp(is->fields[0], "string") == 0) { array[i].type = 's'; array[i].s = strdup(is->fields[1]); } else { exit(1); } } /* Write out the items. */ for (i = 0; i < 5; i++) { printf("Item %d: Type %c -- ", i, array[i].type); if (array[i].type == 'i') { printf("Value: %d\n", array[i].i); } else if (array[i].type == 'f') { printf("Value: %f\n", array[i].f); } else if (array[i].type == 's') { printf("Value: %s\n", array[i].s); } else { exit(1); } } /* Print the size of the item struct */ printf("\n"); printf("Sizeof(Item): %d\n", sizeof(Item)); }