Thread: array of pointers to functions

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    4

    array of pointers to functions

    Hello,

    I must write a programme in which I have to implement a "handler" which will call the apropriate function from a predefined list of function in C/C++, based on the integer which I have given from the command line.

    example: ./program 5 -> will make a system call (a function which is in 5th place in the sys_call_table[])

    I was hoping that someone could help me to start, because I don't know how to begin.

    tnx

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    The idea of function pointers is a bit of a trick, but the syntax is really simple. I hope this helps.

    Code:
    #include <stdio.h>
    
    // You can technically get by without a typedef, but it's rough
    // and noone does it in practice.
    typedef void int_fn (int);
    
    void print_as_digit (int i) {
    	printf ("%d\n", i);
    }
    
    void print_as_char (int i) {
    	putchar (i);
    }
    
    int main (void) {
    	int_fn * function_array[2];
    
    	function_array[0] = print_as_digit;
    	function_array[1] = print_as_char;
    
    
    	// Ok, function_array is now an array of function pointers.
    	// We call function pointers just like how we call functions.
    	function_array[0](64);
    	function_array[1](64);
    
    	return 0;
    }
    Callou collei we'll code the way
    Of prime numbers and pings!

  3. #3
    Registered User
    Join Date
    Oct 2006
    Posts
    4
    I wasn't clear enough. I don't know how to implement an array from which I will call different functions from the address in the array. This array should already be implemented (in c) and i have to call the apropriate function from the address in the array.
    example:
    the address of the array[3] will return printf function
    the address of the array[5] will return some other function like read or something.

  4. #4
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    You mean every part of that array has a function with different return value and different parameters? That's quite difficult but should be possible... somehow...
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  5. #5
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    An array must be homogenous, meaning that all function pointers will likely need to be the same with exception to what they point to.

    > the address of the array[3] will return printf function
    This is a problem. I don't think you can define a function pointer for printf() in the way that you are suggesting. You will need to write a function that works like printf() that uses a finite number of parameters, and point to that.

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >the address of the array[3] will return printf function
    >the address of the array[5] will return some other function like read or something.
    If the functions don't have identical signatures then an array of function pointers may not be an appropriate solution. Why don't you describe what problem you're trying to solve instead of making assumptions about the implementation that you think you need.
    My best code is written with the delete key.

  7. #7
    Registered User
    Join Date
    Oct 2006
    Posts
    4
    I need to implement a function like syscall(int number) to call a system call from a specified address known by the parameter number. this number is one of the defined in /usr/include/asm-i386/unistd but I don't know how. This function will call the pointer to the apropriate function.(i think)

    this is the code I have so far:

    #include <stdio.h>
    #include <stdlib.h>

    /* Array with three pointer to functions */
    void *test1 = /* this is the code I lack */
    void *test2 = /* this is the code I lack */
    void *test3 = /* this is the code I lack */
    void (*functptr[])() = {test1,test2,test3 };

    int main(int argc, char *argv[])
    {
    int choice = atoi(argv[0]);

    (*functptr[choice])(); /* Call desired function */

    return 0;
    }

    how can I make a void pointer to a function ??
    Last edited by moowy; 10-19-2006 at 03:45 PM.

  8. #8
    Registered User
    Join Date
    Oct 2006
    Posts
    4
    never mind... I solved it (for now ). tnx for the help

  9. #9
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >how can I make a void pointer to a function ??
    You can't. Function pointers and void pointers are incompatible. Since this is C++, you'd really be better off using function objects. There's a great deal more flexibility in that.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. MergeSort with array of pointers
    By lionheart in forum C Programming
    Replies: 18
    Last Post: 08-01-2008, 10:23 AM
  2. How to sort an array of pointers to structure
    By broli86 in forum C Programming
    Replies: 3
    Last Post: 06-30-2008, 02:52 PM
  3. Syntax for constant array of array pointers
    By BMintern in forum C Programming
    Replies: 4
    Last Post: 05-14-2008, 08:21 AM
  4. help with functions and array please
    By rebel in forum C++ Programming
    Replies: 4
    Last Post: 11-24-2005, 01:09 PM
  5. Array of struct pointers - Losing my mind
    By drucillica in forum C Programming
    Replies: 5
    Last Post: 11-12-2005, 11:50 PM