Making a lookup table?

This is a discussion on Making a lookup table? within the C Programming forums, part of the General Programming Boards category; How can i create a lookup table in c without using files. for example, if the char value is 0xE4 ...

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    11

    Making a lookup table?

    How can i create a lookup table in c without using files.

    for example, if the char value is 0xE4 change it to 0x37. but there are 256 original differnt values.

    can you help me please.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
    char table[256] = { ... (at position 0xE4} 0x37 ... };
    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,712
    You can hard code an array into your program. If necessary, you can compute the array index from the given char value.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    May 2008
    Posts
    11
    so for example

    Code:
    char table[256] = { ... (at position 0xE4) 0x37, (at position 0xE3) 0x36 ... };
    and just do that for all 256 positions?

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,712
    Yes.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    May 2008
    Posts
    11
    and

    "at position 0xE3" is acceptable C code?

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by username101 View Post
    and

    "at position 0xE3" is acceptable C code?
    No, you need to do something like this:
    Code:
    char c[16] = { 17, 63, 12, 81,   // 0..3 
                           99, 11, 15, 44,   // 4..7
    ...
    Unless you want to restrict yourself to using gcc, which allows some syntax similar to this:
    Code:
    char c[16] = { [0] = 17, [3] = 81, [1] = 63 ... }
    (and ... is not valid syntax, just that I'm too lazy to type in ALL of the data to fill 16 chars - never mind 256, just to show how something works).

    Alternatively, write a program that generates a list of comma separated values, sya "mydata.tbl", and use this:
    Code:
    char table[256] = {
    #include "mydata.tbl"
    };

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    Registered User
    Join Date
    May 2008
    Posts
    11
    thank you i fully understand now

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dynamic struct alignment?
    By matthew180 in forum C Programming
    Replies: 7
    Last Post: 06-15-2007, 08:34 AM
  2. Duplicate at Hash Table
    By DarrenY in forum C Programming
    Replies: 1
    Last Post: 05-10-2007, 02:31 AM
  3. Group Project Help/Volunteer
    By DarkDot in forum C++ Programming
    Replies: 3
    Last Post: 04-24-2007, 11:36 PM
  4. Sorting a table and saving a table help!!!
    By MrMe913 in forum C Programming
    Replies: 4
    Last Post: 04-18-2007, 12:19 PM
  5. MUD Concept Question
    By mrpickle in forum Game Programming
    Replies: 3
    Last Post: 12-01-2003, 11:45 AM

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