Thread: Creating address lookup table

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    1

    Creating address lookup table

    Greetings all. I am fairly new to C (assembler programmer for years), so I have assembler processes in my head - which I am trying to translate to C.
    I need to create a static lookup table which contains the address of a function and a bunch of other static parameters, thus:-

    funct1,0,4,12,0,0,1,7,
    funct2,0,3,6,1,2,7,6,1,
    funct3...etc

    I then have a routine that decides which set of parameters to use. The routine loads in the values from the lookup table into variables - which are used in other parts of the program. The address of the "funct" goes into a pointer "void (*fnptr) (void)".

    So the question(s) are:
    1. How is the table created - what is the syntax for combining different static types?
    2. How is each element in the table accessed by the accessing routine?

    I have tried several different ways but the compiler wont have a bar of it.

    Thanks in advance!

  2. #2
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    Are the parameters fixed length? If yes, create a structure then assign each parameter into an array of that struct and pass a reference to whichever one you need when you need it. As far as the functions are concerned, you can create an array of void * and assign the function names to that array.

    EDIT: A note: You may have to create a typedef to define what the function is in order to use it out of the array of void *

  3. #3
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    Something like this perhaps:
    Code:
    typedef struct {
    	void (*fnptr)(void);
    	int parameters[8];		// maximum
    	} ADDRESS_TABLE_DEF;
    
    void funct1(void);	// prototype
    void funct2(void);	// prototype
    
    ADDRESS_TABLE_DEF address_table[] = {
    	{ funct1, 0, 4, 12, 0, 0, 1, 7 },
    	{ funct2, 0, 3, 6, 1, 2, 7, 6, 1 },
    	};
    But if those parameters are to be passed to the corresponding function, then that function definition should probably not be (void).

    This example shows initialization to the defaults you mentioned. You said "static" a lot, but then you say you want to change the parameters... So I assume you do not want to treat this array of structures as "read only". That's why I didn't incorporate the 'static' attribute. Perhaps you meant global, or fixed in an immovable place in memory.
    Last edited by nonoob; 08-06-2009 at 02:15 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to Send Mac Address From Client to Server
    By Lieyza197 in forum C Programming
    Replies: 2
    Last Post: 05-27-2009, 09:58 AM
  2. quick log lookup table
    By R.Stiltskin in forum C++ Programming
    Replies: 19
    Last Post: 12-04-2008, 12:39 PM
  3. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  4. virtual memory
    By sweets in forum C Programming
    Replies: 6
    Last Post: 11-06-2004, 06:55 AM
  5. MUD Concept Question
    By mrpickle in forum Game Programming
    Replies: 3
    Last Post: 12-01-2003, 12:45 PM