Associative Array in C

Discussion in 'C Programming' started by chadsspameateremail@yahoo.com, Mar 3, 2008.

  1. Guest

    What I am trying to do is implement a associative array to lookup data
    based on a key in C. This might simple to do in C++ or Perl but I'm
    restricted to use C. The size of the data for the table isn't too
    large but too large to structure the code as a clean chain of if/else
    or similar logic. Also, the lookup key values can be large,
    unpredictable and not well dispersed which doesn't suit itself to a
    hash table.

    OK so my current idea for implementing this is using an array of C
    structures which gets initialized and contains all the relevant
    information. The array will be a module global variable used in the C
    file which needs this functionality. The array definition would look
    something like the following:

    struct ErrorLookupTable
    {
    long DeviceErrorCode;
    char* ErrorString;
    };

    #define TableSize 6

    static struct ErrorLookupTable MyTable[ TableSize ] =
    {
    { 45, "Not enough memory" },
    { -66, "File I/O error" },
    { -2565, "Device locked" },
    { 32727, "Device not found" },
    { -32727, "Device not found" },
    { 65534, "Unknown device error" }
    //... LOTS more
    };

    So I have implemented some prototype code based on this and it does
    work to do lookups. But what I was wondering is how portable is
    this? Is this legal C as defined by the standard to initialize an
    array of structures in this way? Is this going to work across
    platforms? I hope you see my dilemma the code appears to work on my
    system. But I don't know if it's guaranteed to work everywhere and how
    portable the library functions I'm working on will be because of it?

    Thanks. :)
     
    , Mar 3, 2008
    #1
    1. Advertising

  2. writes:

    > What I am trying to do is implement a associative array to lookup data
    > based on a key in C. This might simple to do in C++ or Perl but I'm
    > restricted to use C. The size of the data for the table isn't too
    > large but too large to structure the code as a clean chain of if/else
    > or similar logic. Also, the lookup key values can be large,
    > unpredictable and not well dispersed which doesn't suit itself to a
    > hash table.

    Sorry you could use a hash table without troubles. I bet you'd like to
    look uf the Error Code by number and so this would be the key for you
    hash table....

    >
    > OK so my current idea for implementing this is using an array of C
    > structures which gets initialized and contains all the relevant
    > information. The array will be a module global variable used in the C
    > file which needs this functionality. The array definition would look
    > something like the following:
    >
    > struct ErrorLookupTable
    > {
    > long DeviceErrorCode;
    > char* ErrorString;
    > };
    >
    > #define TableSize 6
    >
    > static struct ErrorLookupTable MyTable[ TableSize ] =
    > {
    > { 45, "Not enough memory" },
    > { -66, "File I/O error" },
    > { -2565, "Device locked" },
    > { 32727, "Device not found" },
    > { -32727, "Device not found" },
    > { 65534, "Unknown device error" }
    >

    //... LOTS more
    surely not you have set TableSize to 6 ...



    > Is this legal C as defined by the standard to initialize an
    > array of structures in this way?

    Yes

    >Is this going to work across
    > platforms?

    Yes

    Regards
    Friedrich

    --
    Please remove just-for-news- to reply via e-mail.
     
    Friedrich Dominicus, Mar 3, 2008
    #2
    1. Advertising

  3. wrote:
    > What I am trying to do is implement a associative array to lookup data
    > based on a key in C.

    [...]
    > struct ErrorLookupTable
    > {
    > long DeviceErrorCode;
    > char* ErrorString;
    > };
    >
    > #define TableSize 6
    >
    > static struct ErrorLookupTable MyTable[ TableSize ] =
    > {
    > { 45, "Not enough memory" },
    > { -66, "File I/O error" },
    > { -2565, "Device locked" },
    > { 32727, "Device not found" },
    > { -32727, "Device not found" },
    > { 65534, "Unknown device error" }
    > //... LOTS more
    > };
    >
    > So I have implemented some prototype code based on this and it does
    > work to do lookups. But what I was wondering is how portable is
    > this?


    This code works and is portable, but it can be improved:
    1. Instead of using a macro for the size, leave the size open and instead
    compute the eventual size:
    struct foo array[] = {...};
    size_t const array_size = (sizeof array)/(sizeof *array);
    This has the advantage that you don't have to manually keep the macro and
    the array's size in sync.
    2. A string literal must not be modified, but it will still (compatibility
    with older code) convert to a modifyable pointer. However, you should not
    use that, so you should make 'ErrorString' a 'char const*' instead. Unless
    you need to modify it, you should also make the whole array constant, so
    the compiler tells you when you accidentally try to modify it.
    3. If you sorted the array by the error code, you could use a faster binary
    search, assuming the error code is the key value. I'd actually put in a
    test somewhere like assert(array_is_sorted()); then so you won't forget
    that after code changes.

    Otherwise, this code is completely functional as it is.

    Uli
     
    Ulrich Eckhardt, Mar 3, 2008
    #3
  4. user923005 Guest

    On Mar 2, 8:24 pm, wrote:
    > What I am trying to do is implement a associative array to lookup data
    > based on a key in C.  This might simple to do in C++ or Perl but I'm
    > restricted to use C.  The size of the data for the table isn't too
    > large but too large to structure the code as a clean chain of if/else
    > or similar logic.  Also, the lookup key values can be large,
    > unpredictable and not well dispersed which doesn't suit itself to a
    > hash table.
    >
    > OK so my current idea for implementing this is using an array of C
    > structures which gets initialized and contains all the relevant
    > information.  The array will be a module global variable used in the C
    > file which needs this functionality.  The array definition would look
    > something like the following:
    >
    > struct ErrorLookupTable
    >    {
    >    long  DeviceErrorCode;
    >    char* ErrorString;
    >    };
    >
    > #define TableSize 6
    >
    > static struct ErrorLookupTable MyTable[ TableSize ] =
    >    {
    >       {     45, "Not enough memory" },
    >       {    -66, "File I/O error" },
    >       {  -2565, "Device locked" },
    >       {  32727, "Device not found" },
    >       { -32727, "Device not found" },
    >       {  65534, "Unknown device error" }
    >       //... LOTS more
    >    };
    >
    > So I have implemented some prototype code based on this and it does
    > work to do lookups.  But what I was wondering is how portable is
    > this?  Is this legal C as defined by the standard to initialize an
    > array of structures in this way?  Is this going to work across
    > platforms? I hope you see my dilemma the code appears to work on my
    > system. But I don't know if it's guaranteed to work everywhere and how
    > portable the library functions I'm working on will be because of it?


    If you provided the errors in sorted order, you could use bsearch to
    do the lookup.
    I guess that a hash table will work perfectly well and be the best
    solution for your problem.
    If you don't want to use a hash table, then provided the data in
    sorted order or use a skiplist.

    P.S.
    Besides the good suggestions to use sizeof, I would also suggest
    making everything const.
     
    user923005, Mar 3, 2008
    #4
  5. Guest

    Thanks everyone! :)
    I am going to implement it with the suggestions by Ulrich and others
    especially the use of const and use a sizeof for finding the table
    size which I really like. Sorting I also see as the table continues
    to grow. Obviously you guys have seen this type of problem before.
    Thanks alot! :)
     
    , Mar 3, 2008
    #5
  6. Guest

    Thanks everyone! :)
    I am going to implement it with the suggestions by Ulrich and others
    especially the use of const and use a sizeof for finding the table
    size which I really like. Sorting I also see as the table continues
    to grow. Obviously you guys have seen this type of problem before.
    Thanks alot! :)
     
    , Mar 3, 2008
    #6
  7. CBFalconer Guest

    wrote:
    >
    > What I am trying to do is implement a associative array to lookup
    > data based on a key in C. This might simple to do in C++ or Perl
    > but I'm restricted to use C. The size of the data for the table
    > isn't too large but too large to structure the code as a clean
    > chain of if/else or similar logic. Also, the lookup key values
    > can be large, unpredictable and not well dispersed which doesn't
    > suit itself to a hash table.


    Take a look at what hashlib can do for you. Purely standard C, and
    released under GPL. You don't have to worry about table size. See:

    <http://cbfalconer.home.att.net/download/>

    --
    [mail]: Chuck F (cbfalconer at maineline dot net)
    [page]: <http://cbfalconer.home.att.net>
    Try the download section.



    --
    Posted via a free Usenet account from http://www.teranews.com
     
    CBFalconer, Mar 3, 2008
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. nospam
    Replies:
    2
    Views:
    648
    Mohammd M. Hussain
    Jun 26, 2004
  2. desktop
    Replies:
    5
    Views:
    408
    James Kanze
    Jun 26, 2007
  3. Yvon Thoraval
    Replies:
    5
    Views:
    230
    Jason Creighton
    Sep 17, 2003
  4. VK
    Replies:
    47
    Views:
    594
    Thomas 'PointedEars' Lahn
    Jul 13, 2005
  5. VK
    Replies:
    36
    Views:
    688
    Martin Honnen
    Aug 3, 2005
Loading...

Share This Page