table of functions

This is a discussion on table of functions within the C Programming forums, part of the General Programming Boards category; Hi, I'm having difficulty visualising how to solve this. Currently I have a function that looks up a value inside ...

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    7

    table of functions

    Hi,
    I'm having difficulty visualising how to solve this.

    Currently I have a function that looks up a value inside a table like so:

    value = index_table[i];

    However **without changing** the above line I'd like to call a function instead with the param i. In effect swapping the index_table for a function.

    I have two ideas:
    1: Swap the table completely by using a #define to remap the table to a function.
    2: Keep the table but for each element in the table (table is not so big) call the function with a corresponding param.

    I can not seem to manage for either way currently.
    Is one or both of the ideas suitable or I'm I missing something more obvious?
    Can someone point me to an example or show me a simple example?

    Please remember I can not change value = index_table[i];

    It'd be appreciated!!

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I'm not sure what it is you're exactly trying to do. Why can't you change the line? You don't really explain well what 'index_table' is supposed to be. Are you trying to have it call a different function for each value of i? So:
    Code:
    index_table[ 0 ]; // calls one function
    index_table[ 1 ]; // calls a different function
    Does it call the same function, but just pass it 0 or 1? What eactly. And why can't you change the line? Is this just some stupid homework thing your teacher thought up? "Don't change this, but make it do something else..."?


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

  3. #3
    Registered User
    Join Date
    Apr 2005
    Posts
    7
    Thanks for the quick reply.

    >Why can't you change the line?
    This line of code is in another module which is legacy code and is used in many different programs. I'm trying to not touch this for my circumstance. Of course if it not possible to do this then I will have to change this but really that is my last resort. However I believe it can be done without changing the line (easy said when I can't get it to work, i know!)

    >Are you trying to have it call a different function for each value of i?
    >Does it call the same function, but just pass it 0 or 1?
    The same function but with 0 or 1. However wouldn't it be the same method in either case?

    Also re-reading my original post, I should also clarify that the function must also return a value. This can be seen in the one line of code but I didn't outline it explicitly.

    Thanks

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,440
    Your constraint not to change the line "value = index_table[i];" makes what you're trying to do unachievable in C.

    You could get a partial solution in C++ by making index_table an object of some type that has an operator[] defined. The catch with that, naturally enough, is that it won't work with a vanilla C compiler.

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I still don't see why you can't change the line. You're already going to have to recompile all of your other modules. Even if you made this an object in C++ and overloaded the [] operator, you'd still end up having to recompile the other modules. So again, I don't see how you think you're better off not changing [ ] to ( ).


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

  6. #6
    B26354 Deckard's Avatar
    Join Date
    Jan 2002
    Posts
    633
    I'd like to get a better understanding of why the legacy function won't work for you as-is. I'm guessing that you've decided index_table needs to be an array of function pointers because it solves a particular problem. Perhaps there is another way of solving that problem that doesn't require you to change the legacy function. Can you post some code for us to look at?
    Last edited by Deckard; 08-11-2005 at 09:46 AM. Reason: Whoa whoa whoa
    Jason Deckard

  7. #7
    Registered User
    Join Date
    Apr 2005
    Posts
    7
    @quzah
    Of course everything must be re-compiled, this is not the problem. The problem is that the module with
    Code:
    value = index_table[i];
    is used in other programs. If i change this module then all other programs which make use of this module also have to change. Alternatively they must branch which is also not desireable. I don't want to get into a discsussion about SW reuse but I do not have the freedom to change/edit anything I like.

    @Deckard
    Your response did make me think about what new functionality I wanted and if it could not be done inside a table.
    Below is a simple example:

    Orginal table
    Code:
    static const u8 index_table[] =
    {
      0x01,
      0x02,
      0x03,
    }
    However now I'd like to index into another table from this table based on the value of [i]. Something like:

    Code:
    static const u8* index_table[] =
    {
      new_index_table,
      new_index_table,
      new_index_table,
    }
    
    static const u8 new_index_table[] =
    {
      0x05,
      0x06,
      0x07,
    }
    so using
    Code:
    value = index_table[i]; //when i=2, value will equal 0x06
    Hope this clarifies what I would like to do.

    Thanks for the help.

  8. #8
    B26354 Deckard's Avatar
    Join Date
    Jan 2002
    Posts
    633
    If the legacy code returns value to your code (and I don't know if it does), would it make sense to keep the second table in your new code? You could get the result of the first table by calling the legacy code and then look up the corresponding number in your new table.
    Code:
    index = legacyFunction( i ); /* Contains the 'value = index_table[i]' statement */
    value = new_index_table[index];
    Does any of the other code using the legacy function need the new table?
    Jason Deckard

  9. #9
    Registered User
    Join Date
    Jun 2004
    Posts
    4
    Deckard,
    I'm a little confused by your reply. I'm afraid I'm not doing a good job of explainaing the situation.

    The code
    Code:
    value = index_table[i];
    is inside a function which i can 'not' touch. However the actual index_table[] is in my module and I have complete control over this.
    The variable value is used by the legacy code and this code expects a u8 value and that is all.
    I initially wanted to change this as I need to process the data differently before it is returned to the legacy function. My idea was to perform this processing in a function hence why I was trying to use the table to point to a function passing differnt values into the function to process and also return the value.
    However afer looking at the function which i wrote, it is not doing anything too clever. In fact it's only really looking up an entry in one table based on a value in another table (plus some masking). Therefore the solution is much simpler than my initial post was asking.
    Thanks for the help anyway!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Style Points
    By jason_m in forum C Programming
    Replies: 4
    Last Post: 05-28-2008, 06:15 AM
  2. progarm doesnt compile
    By kashifk in forum Linux Programming
    Replies: 2
    Last Post: 10-25-2003, 05:54 PM
  3. extra word printing
    By kashifk in forum C++ Programming
    Replies: 2
    Last Post: 10-25-2003, 04:03 PM
  4. inputting words from a file
    By kashifk in forum C++ Programming
    Replies: 5
    Last Post: 10-24-2003, 07:18 AM
  5. rehash help
    By kashifk in forum C++ Programming
    Replies: 1
    Last Post: 10-22-2003, 06:51 PM

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