Nope. As you've found out, you can't. The compiler needs to know the size of its data structures, reliably and constantly, to do its work.
I'm assuming that the page data is not the only content of your struct
. You probably have some header / management information about your page in there along with the text.
The common solution would be for your struct to contain a char *
pointing at the actual text. That text could then be in a chunk of memory malloc()
'd or calloc()
'd to whatever size you need. Alas, this means you'll have to remember to move the external text around along with your struct whenever you do operations that move the struct around, and memory manage the text when you get rid of the struct... the usual C headaches.
EDIT
There are sensible "standard" ways to do this kind of thing. Here is a suggestion:
typedef char *page_ptr;
int page_size = discover_page_size();
int max_page_count = discover_max_page_count();
int pages_stored = 0;
page_ptr *page_pointers = calloc(max_page_count, sizeof(page_ptr));
char *pages = calloc(max_page_count * page_size, sizeof(char));
page_pointers[0] = pages;
int i;
for (i=1; i<max_page_count; i++) {
page_pointers[i] = page_pointers[i-1] + page_size;
}
Now you can treat page_pointers as an array of pages, doing stuff like
read(filedesc, page_pointers[page_count++], page_size);
or
memmove(page_pointers[69], page_pointers[42], page_size);
You have an overhead of one pointer per page (surely acceptable), and your pages are physically contiguous in memory. What more could you ask? :)