Thread: get string value from 2d table

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

    get string value from 2d table

    .
    Code:
        void (*const state_table [5][8]) () = {
        
         { answer, nothing, nothing, nothing, nothing, nothing, nothing, nothing}, /* procedures for state 0 */
         { nothing, play, end, nothing, nothing, nothing, nothing, nothing}, /* procedures for state 1 */
         { nothing, nothing, nothing, nothing, end, end, nothing, nothing}, /* procedures for state 2 */
         { nothing, nothing, nothing, nothing, nothing, end, end, nothing}, /* procedures for state 3 */
         { nothing, nothing, nothing, nothing, nothing, nothing, nothing, nothing} /* procedures for state 4 */
        };
    
    
    
        printf("%s\n", state_table[1][2]);
    How can I get the string value from a 2d table. For example from state_table, one row down and one column down, string "play"

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    1) string literals in C must be double quoted.
    2) I have no idea what this is supposed to be:
    Code:
    void (*const state_table [5][8]) ()
    but it looks like a function pointer prototype, not a 2D array of string literals. How about:

    Code:
    const char *state_table[5][8] = {
      { "answer", "nothing", "nothing", "nothing", "nothing", "nothing", "nothing", "nothing"},
     { "nothing", "play", "end", "nothing", "nothing", "nothing", "nothing", "nothing"},
     { "nothing", "nothing", "nothing", "nothing", "end", "end", "nothing", "nothing"},
     { "nothing", "nothing", "nothing", "nothing", "nothing", "end", "end", "nothing"},
     { "nothing", "nothing", "nothing", "nothing", "nothing", "nothing", "nothing", "nothing"}
    };
    Now your printf will work.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    Oct 2011
    Posts
    22
    yes it is function pointer prototype - I didnt know what is was called. What I really need is to log the functions that are called by the function pointer.

    Now that you have shown me the proper way to do it as a 2d table I can do that however this will require to duplicate the structure of the function pointer.

    Do you have any suggestion if the called function can be logged from with the function pointer?

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    I'm not sure what you mean. If you mean you want to pass a pointer to this table to a logging function, the parameter would be:

    Code:
    void my_logger (const char *table[5][8]);
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  5. #5
    Registered User
    Join Date
    Oct 2011
    Posts
    22
    Let me try to explain a bit further. is it possible to put further code inside the function pointer prototype to printf out the name of the called function each time it does a lookup

  6. #6
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Quote Originally Posted by robi View Post
    Let me try to explain a bit further. is it possible to put further code inside the function pointer prototype to printf out the name of the called function each time it does a lookup
    Not easily. At least, not if you want to have an "automatic" way to have the strings table mirror the function pointer table. You can do some complicated pre-processor trickery with X-Macros (C preprocessor - Wikipedia, the free encyclopedia), but that's a pain. The other alternative is simply to use the __func__ symbol and put a printf/log statement in each function:

    Code:
    void answer(void)
    {
        fprintf(log_file, "Executing function %s\n", __func__);
    
        // the rest of your function
    }

  7. #7
    Registered User
    Join Date
    Aug 2010
    Posts
    231
    Quote Originally Posted by MK27 View Post
    2) I have no idea what this is supposed to be:
    Code:
    void (*const state_table [5][8]) ()
    Its a 2dim state machine from a thread last week.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. localization with string table
    By techi_talk in forum Windows Programming
    Replies: 2
    Last Post: 10-15-2005, 02:16 PM
  2. String Table Help
    By Frantic- in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2005, 12:04 PM
  3. String Table Question
    By DominicTrix in forum Windows Programming
    Replies: 3
    Last Post: 12-10-2004, 02:50 PM
  4. Can't load string from resource DLL's string table
    By s_k in forum Windows Programming
    Replies: 4
    Last Post: 07-15-2003, 06:43 AM
  5. Reading values from a string table..
    By Dual-Catfish in forum Windows Programming
    Replies: 1
    Last Post: 06-28-2002, 05:22 PM