Thread: Making a lookup table?

  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
    28,413
    You can hard code an array into your program. If necessary, you can compute the array index from the given char value.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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
    28,413
    Yes.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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, 12:45 PM