Table mapping Strings to Strings

This is a discussion on Table mapping Strings to Strings within the C Programming forums, part of the General Programming Boards category; Hi, I'm looking to create a table that will map strings to strings. The format will basically be: --------------------------------------- | ...

  1. #1
    Registered User
    Join Date
    Sep 2003
    Posts
    2

    Question Table mapping Strings to Strings

    Hi,

    I'm looking to create a table that will map strings to strings. The format will basically be:
    ---------------------------------------
    | Name | Description |
    ----------------------------------------
    | name1 | some info |
    | name 2 | other info |

    The only way I can think of doing this at the moment is by maintaining three arrays.
    char name_array[Max_entries][Max_string_length];
    char info_array[Max_entries][Max_string_length];
    char** ptr_array[Max_entries][2];

    so that when I add a record to name_array and info_array, I pass a pointer to the record I just added into ptr_Array. e.g.

    ptr_array[x][1] = &name_array[i];
    ptr_array[x][2] = &info_array[j];

    Will this even work, or is there an easy way of doing it?

    Any help appreciated.

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,794
    If you used C++ then it would be extremely easy. You would just use a map<string,string> container. Is that an option or must it be vanilla C?
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    I'd probably use a struct to make things easier to read:
    Code:
    typedef struct Entry
    {
      char *pName;
      char *pDescription;
    } Entry;
    
    Entry Table[100];
    
    Table[0].pName = pAName;
    Table[0].pDescription = pADescription;
    But it's your choice...
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  4. #4
    Registered User
    Join Date
    Sep 2003
    Posts
    2
    Hi,

    C++ isn't an option for legacy reasons. I reckon I'll use the struct suggested in a linked list.


    Thanks for the help.

  5. #5
    Registered User
    Join Date
    Jul 2003
    Posts
    110
    Have you considered using a hash table, or do you only have a few items to look up?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help using strings and mapping
    By trprince in forum C Programming
    Replies: 29
    Last Post: 12-01-2007, 03:01 PM
  2. Writing array, to file
    By zootreeves in forum C Programming
    Replies: 9
    Last Post: 09-08-2007, 05:06 PM
  3. extra word printing
    By kashifk in forum C++ Programming
    Replies: 2
    Last Post: 10-25-2003, 04:03 PM
  4. inputting words from a file
    By kashifk in forum C++ Programming
    Replies: 5
    Last Post: 10-24-2003, 07:18 AM
  5. saving strings to a table
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 03-14-2002, 10:47 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21