Thread: Creating Dynamic Structure in C

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    1

    Creating Dynamic Structure in C

    Sir
    I want to create a DBMS package in C. Now what problem I am
    facing that I am creating a .DBF file structure at runtime and storing it in a file (say EMP.DBF) . This file contains the Field names, field types and Field Width. Now the structure may be
    anything. say (for Student, Employee, Airlines) with different structure. Now I need C code to know how to create a dynamic structure which will be created by reading the file structure stored in the file and to maintain a concerned linked list for DML.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Some thoughts
    Code:
    typedef enum {
        isInt,
        isString,
        isDouble,
    } data_et;
    
    typedef struct field_st_tag {
        char                *fieldName;
        data_et              dataType;
        struct field_st_tag *next;
    } field_st;
    
    typedef struct data_st_tag {
        struct field_st_tag *field; /* what describes this field */
        union {
            int     iValue;
            char   *sValue;
            double  dValue;
        } u;                        /* all possible data types */
        struct data_st_tag  *next;  /* the next field */
    } data_st;
    
    typedef struct data_row_st_tag {
        struct data_st_tag      *row;   /* first field on this row */
        struct data_row_st_tag  *next;  /* the next row */
    } data_row_st;
    So you would read the file, and create a linked list of fields.
    For StudentName:string, StudentAge:int, you would allocate and initialise
    two nodes of a linked list
    { "StudentName", isString },
    { "StudentAge", isInt }

    You would then read the data file one record at a time, and then walk
    your fields list to decide what type each record field should be.
    Allocate a data_st and initialise it with the correctly converted result.

    Use data_row_st to build up a linked list of linked lists of all your data.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 02-14-2006, 09:04 AM
  2. creating a dynamic 2d array?
    By der dom in forum C Programming
    Replies: 11
    Last Post: 09-28-2004, 05:16 PM
  3. Dynamic Structure Array Help
    By B_D_C in forum C Programming
    Replies: 8
    Last Post: 10-14-2003, 09:04 AM
  4. operator overloading and dynamic memory program
    By jlmac2001 in forum C++ Programming
    Replies: 3
    Last Post: 04-06-2003, 11:51 PM
  5. creating dynamic arrays
    By Cnewb in forum C Programming
    Replies: 11
    Last Post: 12-01-2002, 06:13 PM