Thread: Variable Data Structure

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    22

    Variable Data Structure

    I am going to have a data structure that stores a name, and associated with this name will be a list of codes.

    However, there can be any number of codes in this list associated to the name.

    I am going to use a data struct too hold the data - I know this much, and know how to do this.

    However, I do not know how to handle the codes, of which there can be any number. I think an array is out of the question (cannot be resized?)


    Any ideas?

    Thanks;

    Jamie

  2. #2
    Registered User
    Join Date
    May 2006
    Posts
    22
    (Eg. an array of strings)

  3. #3
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    Look into the malloc command for dynamic memory allocation.
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

  4. #4
    return 0;
    Join Date
    Jan 2005
    Location
    Netherlands
    Posts
    89
    You can create an array using malloc() and resize it using realloc()

    Code:
    struct Data
    {
        int foo;
        int bar;
    };
    
    // Create an array which can hold 100 struct Data
    struct Data *array = malloc(sizeof(struct Data) * 100);
    if(array == NULL) {
        // Error, can't create array
    }
    
    // Resize the array so it can hold 200 struct Data
    struct Data *resized = realloc(array, sizeof(struct Data) * 200);
    if(resized == NULL) {
        // Error, can't resize array
    }
    else {
        array = resized;
    }
    
    // Be sure to free the memory when your program terminates or when you no longer need the array
    
    free(array);

  5. #5
    Registered User
    Join Date
    May 2006
    Posts
    12
    One way is to use linked lists
    Code:
    typedef struct tag_linked_list_node
    {
        char *code; //use malloc to allocate memory for code or write char code[SMALL_SIZE]; or int code;
        tag_linked_list_node *pNext;
    }LLIST_NODE, *PLLIST_NODE;
    
    struct s{
        char* name; //use malloc to allocate memory for name
        PLLIST_NODE pFirst;
    };
    You can find and read hundred articles about linked lists.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dynamic Array substitution in composite data variable
    By DavidDobson in forum C Programming
    Replies: 2
    Last Post: 08-12-2008, 04:29 AM
  2. data structure design for data aggregation
    By George2 in forum C# Programming
    Replies: 0
    Last Post: 05-20-2008, 06:43 AM
  3. Help require for some data structure topics
    By jawwadalam in forum A Brief History of Cprogramming.com
    Replies: 10
    Last Post: 12-15-2002, 07:09 PM
  4. can't insert data into my B-Tree class structure
    By daluu in forum C++ Programming
    Replies: 0
    Last Post: 12-05-2002, 06:03 PM
  5. C Programming Question
    By TK in forum A Brief History of Cprogramming.com
    Replies: 13
    Last Post: 07-04-2002, 07:11 PM