/****************** my illustration of encoding data structures into arrays for writing to disk and retranslating to structs. ***********/ /*********** First some C to create a circular linked list. *****************/ #include #include #include typedef struct cell { int type; char contents; struct cell * next;} * cell_t; typedef struct printable_cell { int type; char contents; int next;} * printable_cell_t; printable_cell_t make_printable( cell_t); void print_structs( printable_cell_t p_arr[], int N); main() { int i; cell_t head, mid, last; printable_cell_t p_arr[3]; head = (cell_t) malloc (sizeof (struct cell)); mid = (cell_t) malloc (sizeof (struct cell)); last = (cell_t) malloc (sizeof (struct cell)); head-> type = 0; head->next = mid; head ->contents = 'A'; mid -> type = 0; mid ->next = last; mid ->contents = 'B'; last-> type = 0; last->next = head; last ->contents = 'C'; printf("%c %c %c %c\n", head->contents, head->next->contents, head->next->next->contents, head->next->next->next->contents); /* array of printable structs*/ /* the following is NOT NOT NOT an acceptable general solution to creating your array of printable struct pointers! It's a special-purpose hack to show what your general solution would have done elegantly!*/ p_arr[0] = make_printable(head); /* p-structs into array */ p_arr[1] = make_printable(mid); p_arr[2] = make_printable(last); p_arr[0] -> next = 1; /* correct index ints into p-structs*/ p_arr[1] -> next = 2; /* here by magic, you have to do in general*/ p_arr[2] -> next = 0; print_structs( p_arr, 3); /*also cheating-- in general you have to count structs...don't know how many in advance */ free(head); free(mid); free(last); /*obsessive cleanliness...*/ for (i=0;i<3; i++) { free (p_arr[i]);} } /* generally we'll need one of these conversion routines for each struct type since it needs to know what to do with the struct's fields */ printable_cell_t make_printable(cell_t c_t) { printable_cell_t pcell_t; pcell_t = (printable_cell_t) malloc (sizeof (struct printable_cell)); pcell_t -> contents = c_t->contents; pcell_t -> type = c_t->type; pcell_t -> next = -99; /*not filled in yet*/ return (pcell_t); } /* likewise a structure type's print routine needs to know about the types of its fields so it can do the right thing (like print an array if necessary!)*/ /* below could take a file pointer, use fprintf, and write to disk*/ void print_structs( printable_cell_t p_arr[], int N) { int i; printf ("\n%d",p_arr[0] ->type); /*type of structs this array*/ printf ("\n%d",N); /*how big is array*/ for (i = 0; icontents, p_arr[i]->next);} printf ("\n"); } /************ running this program prints out A B C A 0 3 0 A 1 1 B 2 2 C 0 Your code would take the head pointer, determine how many structs of what type are out there and make a printable struct for each, with an array for each type of struct whose elements are pointers to the printable version. Each printable struct has a printable version of its contents except for its pointers, for which ints are used as indices (pointer equivalents) into the same (or other) arrays of printable structs (depending on the type of structs pointed to.) Here we only have one type, and thus one array of three pointers to the three printable structs. So the arrangment now looks like this, with ----> denoting pointer to struct and [..] denoting the struct. type 0 printable struct array printable structs index array contents (char contents field) (next field (now an int)) 0 -------> [ 'A' 1 ] 1 -------> [ 'B' 2 ] 2 -------> [ 'C' 0 ] So you see the ints in the next field considered as indices into the printable struct array have the same information as the pointers in the original linked list -- they identify corresponding structs. So what gets written onto disk is enough to allow you to reconstruct the above situation: maybe something like this 0 (type) 3 (number of rows) 0 'A' 1 (rows in order) 1 'B' 2 2 'C' 0 (The initial 0,1,2 in rows 0, 1, 2 are unnecessary, but maybe helpful). Your re-creation program knows about the fields of all your struct types so it knows in this case that the ints are all indices in the same array. Generally each column of these pointer-index-ints refers to the same array, but different columns can refer to different arrays. At this point it would be completely unambiguous and efficient to write out the next type number, the number of rows in its matrix, and then those "rows". of course each "row" is really a sequence of printable (and readable!) objects that may be more or less complex (like vectors or matrices). *************/