#INCLUDE "STDIO.H" #INCLUDE "STRINGS.H" /* ÐROGRAM TO SET UP USER ALLOCATED RECORD STRUCTURE, SETS UP MEMORY FOR A SINGLE RECORD OF CHARACTERS WITH A POINTER TO IT. ÁN ARRAY OF UNSIGNED CONTAINING LENGTH OF EACH FIELD AND ANOTHER ARRAY OF UNSIGNEDS CONTAINING OFFSETS INTO THE RECORD FOR EACH FIELD. ÅACH FIELD IS PADDED BY ONE BYTE TO ALLOW SPACE FOR \0 NULL TERMINATED FIELDS. Á THIRD ARRAY, OF POINTERS TO CHAR HOLDS POINTERS TO STRINGS CONTAINING FIELD NAMES*/ #DEFINE ÂÕÆÆÓÉÚÅ 81 /*SIZE OF INPUT BUFFER*/ #DEFINE ÆÉÅÌÄÓ 255 /*MAXIMUM FIELDS PER RECORD*/ #DEFINE ÒÅÃÓÉÚÅ 4096 /*MAXIMUM RECORD LENGTH*/ MAIN() Û CHAR INBUFF[ÂÕÆÆÓÉÚÅ]; /*INPUT BUFFER*/ CHAR *MALLOC(), *CALLOC(); /*DECLARE MEMORY ALLOCATE FUNCTIONS*/ CHAR *RECORD; /*POINTER TO RECORD*/ CHAR *FIELDNAMES[ÆÉÅÌÄÓ]; /*ARRAY OF POINTERS TO FIELDNAMES*/ INT *FIELDARRAY; /*CONTAINS FIELD SIZES*/ INT *FIELDPNT; /*CONTAINS FIELD OFFSETS*/ INT NO¤FIELDS; /*NUMBER OF FIELDS IN RECORD*/ INT I, J; /*INDEX & COUNTER*/ INT RECSIZE=0; /*RECORD LENGTH*/ /*SET UP OUR USER DEFINED RECORD STRUCTURE*/ PRINTF("ÅNTER NUMBER OF FIELDS: "); SCANF("%D", &NO¤FIELDS); /*ALLOCATES MEMORY FOR ARRAY TO HOLD FIELD LENGTHS CAST OPERATOR (UNSIGNED*) DEFINES FIELDARRAY AS POINTER TO ARRAY OF UNSIGNED*/ FIELDARRAY = (INT*) CALLOC(NO¤FIELDS * SIZEOF(INT)); /*ALLOCATES MEMORY FOR ARRAY TO HOLD OFFSETS*/ FIELDPNT = (INT*) CALLOC(NO¤FIELDS * SIZEOF(INT)); /* GET FIELD NAMES AND SIZES. NO TESTING IS DONE TO ENSURE RECORD IS <= ÒÅÃÓÉÚÅ, AND NO¤FIELDS <= ÆÉÅÌÄÓ. IT CAN BE ADDED EASILY*/ FOR(I=0; I < NO¤FIELDS; I++) Û PRINTF("ÅNTER NAME FOR FIELD %D: ", I+1); GETS(INBUFF); /*SET ARRAY POINTER TO FIELDNAME STRING*/ FIELDNAMES[I] = MALLOC(STRLEN(INBUFF)+1); /*COPY STRING TO MEMORY ALLOCATED BY MALLOC()*/ STRCPY (FIELDNAMES[I], INBUFF); PRINTF("ÅNTER LENGTH OF FIELD %D: ",I+1); SCANF("%D", &J); J += 1;/*ADD 1 TO SIZE FOR '\0'*/ *(FIELDPNT+I) = RECSIZE; /*OFFSET OF EACH FIELD INTO RECORD*/ *(FIELDARRAY+I) = J-1; /*SIZE IN CHARS OF EACH FIELD*/ RECSIZE += J; /*ADD FIELD LENGTH TO RECSIZE*/ Ý PRINTF("ÒECORD SIZE=%D\N", RECSIZE); /* ALLOCATE STORAGE FOR RECORD */ RECORD = MALLOC(RECSIZE); /*NOW FILL FIELDS IN RECORD*/ PUTCHAR('\N'); FOR(I=0; I < NO¤FIELDS; I++) Û PRINTF("%S: ",FIELDNAMES[I]);/*PROMPT WITH FIELDNAME*/ GETS(INBUFF);/*GET FIELD INPUT*/ /*TRUNCATES STRING TO FIELD LENGTH SPECIFIED*/ IF(STRLEN(INBUFF) > *(FIELDARRAY+I)) INBUFF[*(FIELDARRAY+I)] = '\0'; STRCPY(RECORD+(*(FIELDPNT+I)),INBUFF); Ý /*NOW PRINT CONTENTS OF RECORD*/ FOR(I=0; I < NO¤FIELDS; I++) Û PUTS(RECORD+(*(FIELDPNT+I))); Ý Ý