/******************************************** Dynamic Allocation Written by Jose Mari Reyes Copyright(C)1999, Jose Mari Reyes ********************************************/ #include #include #define MAXSIZE 5 struct Collection { char data[100]; int flag; }; void chopdata(struct Collection **in, char *src) { int i = 0; int idx = 0; int cnt = 0; int sizer = 0; struct Collection *col = *in; /*first allocation of memory multiple of 4 bytes*/ col = malloc (MAXSIZE * sizeof(struct Collection) * 4); while (src[i]) { idx = 0; while (src[i] != ' ' && src[i] && src[i] != '\n' && src[i] != '\r') { col[cnt].data[idx] = src[i]; col[cnt].flag = 1; i++; idx++;col[cnt].data[idx] = '\0'; } while (src[i] == ' ' || src[i] == '\n' || src[i] == '\r' ) i++; cnt++; sizer++; /*resize the collection*/ if (sizer >= MAXSIZE - 2 && src[i]) { realloc(col, MAXSIZE * sizeof(struct Collection) * 4); sizer = 0; } } col[cnt].data[0] = '\0'; col[cnt].flag = 0; *in = col; } void main() { char data[] = "Joey Rocketeers Tiffany\n \rHydra 1 2 10 11 \r\n JetMan"; int cnt = 0; struct Collection *col; printf("\ndata = %s\n\n",data); chopdata(&col, data); while (col[cnt].flag) { printf("\ncol[%d].data = %s",cnt, col[cnt].data); cnt++; } getchar(); free (col); }