From: | Gabriele Svelto |
Date: | 20 Aug 99 at 22:45:20 |
Subject: | Re: Dynamic Database |
From: Gabriele Svelto <jlpicard@tiscalinet.it>
Hello Donald
On 20-Ago-99, you wrote:
> From: DonaldWM@stonelawdrive.freeserve.co.uk
>
> For the past six months, I have been trying to think of a way to create a
> database, which like commercial ones, can accept user input on the
> required structure and then create it all ready for input. I have a
> (nearly) full understanding of C and C++ but can't solve this problem.
> Does anyone know if it is actually possible to do this at all? I include a
> fuller explanation and example below.
>
> /* Dynamic Database idea and code Copyright (C) DWM Productions 1999. The
> code is supposed to begin with an array of char and an array of int.(This
> program would be implemented in Storm C/C++ in Intuition). The char array
> will contain the names of the fields and the int array will contain the
> type (identified by a number). The type will be decided by a radio button
> gadget. The user chooses the button which indicates the required type.
> Each button would be assigned a number which would be entered in the the
> array of int. When creating the record structure when the program is
> running, an array of char would be declared to hold the name of the field
> and another variable would be created according to the previously chosen
> type. Once a basic record structure was created, an array or like
> structure would be created to hold many records of the created type.
>
> I know how to create the intuition window, gadgets, and all about C and
> C++ 's dynamic data structures, etc., but I don't know if C or C++ allow
> variables and structures to be just invented with no prior declaration. I
> have created the program sketch below to show the general idea. */
>
> #include <various header files>
>
> void
> main()
> {
> int no_of_fields = 0;
> int types[100];
> char names[255];
>
> // Create window and gadgets
> // Open window
> // Ask user for number of fields
> // Pass number of fields, etc. to structure_create function
> structure_create(no_of_fields, types, names);
> // Perform other database tasks with array of structures
> // End program
> }
>
> structure_type
> structure_create(int no_of_fields, int types[], char names[])
> {
> // I know the parameters probably aren't comprehensive enough but
that's
> part of the problem.
> // User enters first field name and chooses type
> // Information entered into respective arrays
> .
> .
> // User enters last field name and chooses type
> // Information entered into respective arrays
>
> // *** This is the part I'm unsure of ***
>
> // Array of char created at correct size to hold name of field
> // Variable of selected type created
> .
> .
> // When all complete, record structure is declared and all previous
> variables added to it as structure properties
> // Array or tree, etc. of structure is created
> // *** Function returns array of structures and ends
> return(structure_type);
> }
>
If I'm not wrong you need to create a structure which depends on the user
input, for example it could contain array fo chars of different sizes,
different vars, etc... Since in C/C++ (as far as I know) you're not allowed
to create structures while executing the code you could create a 'general
purpose' structure containing only pointers to all the possible types of
data and allocating memory dinamically only for the needed fields. Even if
you'll lose some memory for the unused pointers it won't be very much
unless you use the structures only for holding a very small amount of
data...