Thread: lookup table mappings

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    22

    lookup table mappings

    Code:
        enum states { STATE_1, STATE_2, STATE_3, STATE_4, STATE_5, STATE_6, STATE_7, STATE_8, STATE_8, MAX_STATES } current_state;
        enum events { EVENT_1, EVENT_2, MAX_EVENTS } new_event;
    
    
        void action_s1_e1 (INFO *info);
        void action_s2_e1 (INFO *info);
        void action_s3_e1 (INFO *info);
        void action_s4_e1 (INFO *info);
        void action_s5_e1 (INFO *info);
        void action_s6_e1 (INFO *info);
        void action_s7_e1 (INFO *info);
        void action_s8_e1 (INFO *info);
        void action_s9_e1 (INFO *info);
    
        void action_s2_e2 (INFO *info);
        void nothing();
    
        enum events get_new_event (INFO *info);
    
    
        void (*const state_table [MAX_STATES][MAX_EVENTS]) (INFO *info) = {
         { action_s1_e1}, {nothing},/* procedures for state 1 */
         { nothing}, {action_s2_e2},/* procedures for state 2 */
         { nothing}, {nothing},/* procedures for state 3 */
        { nothing}, {nothing},/* procedures for state 4 */
         { nothing}, {nothing},/* procedures for state 5 */
         { nothing}, {nothing},/* procedures for state 6 */
         { nothing}, {nothing},/* procedures for state 7 */
         { nothing}, {nothing}, /* procedures for state 8 */
         { nothing}, {nothing}/* procedures for state 9 */
         };

    Can anyone help me figure out this out, is this method called table lookup's?.

    Any sugestions why it not working for me. I am not understanding the mapping. The action procedures in my test code above should be, at state 1 run function action_s1_e1, at state 2 run function action_s2_e2.

    Function action_s1_e1 runs however function action_s2_e2 does not

    I don't think I have the mapping correct because I tested the events enum values sent to state_table function by printing the value which were .0 and 1 which is what it should be.


    I found this code at Writing Efficient State Machines in C

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    We can't actually debug your problem without seeing the rest of the code. It certainly has a lot of differences from the website you took it from, but I'm guessing that's intentional. What don't you understand about the mapping? Your mapping relies on both the current state and the next event to handle, so you need to do your look up using both of those values. Are you sure get_new_event is returning EVENT_2 when you're in state 2? Let's see that full code so we can sort this out.

  3. #3
    Registered User
    Join Date
    Oct 2011
    Posts
    22
    I did it wrong

    Code:
    { nothing}, {nothing},
    Correct is like this
    Code:
    { nothing, nothing},

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by robi View Post
    I did it wrong

    Code:
    { nothing}, {nothing},
    Correct is like this
    Code:
    { nothing, nothing},
    No actually both will work, but the first one was actually correct and the second one will produce a warning with some compilers.
    Are you actually looking up EVENT_2 when looking up the second state? Show the code that uses this.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by iMalc View Post
    No actually both will work, but the first one was actually correct and the second one will produce a warning with some compilers.
    Are you actually looking up EVENT_2 when looking up the second state? Show the code that uses this.
    No. Initializing 2D arrays should go like so:
    Code:
    type foo[ X ][ Y ] =
    {
        { y0, y1 }, /* x0 */
        { y0, y1 }, /* x1 */
        ...
    };
    Wrapping each one of those with their own parenthesis treats them all as xN instead of yN elements:
    Code:
    type foo[ X ][ Y ] = 
    {
        {y0 }, {y1 },
        {y0 }, {y1 },
        ...
    }; /* actually makes x0 through x3 */

    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Okay I don't know WTF I was thinking!
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Table Help
    By BB89 in forum C Programming
    Replies: 15
    Last Post: 10-10-2009, 11:13 PM
  2. Table Help
    By AbbassM in forum C++ Programming
    Replies: 1
    Last Post: 04-02-2009, 08:10 AM
  3. ADT Table
    By MyglyMP2 in forum C++ Programming
    Replies: 2
    Last Post: 05-04-2007, 05:19 AM
  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. Curses - key mappings
    By Mario F. in forum C++ Programming
    Replies: 5
    Last Post: 11-20-2006, 05:37 PM