Thread: declaring pointer variable to array of function pointers

  1. #1
    Registered User
    Join Date
    Mar 2008
    Location
    India
    Posts
    147

    declaring pointer variable to array of function pointers

    I am having following pointer variable declaration

    Code:
    Static double (*funcs[]) (double) = { sin,cos,tan,asin,acos};
    Here funcs is an array of pointers to functions which take double as input and gives double as output with static as storage type am I right.

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Ask your compiler! And try these steps:

    Write down the function prototype that matches the pointer type you want. Since you are using math functions, that is
    double cos(double);

    Now add parens around the name and an asterisk.
    double (*cos)(double);

    Change the name of the function to an alias and make it a typedef.
    typedef double (*MathFunction)(double);

    Now a declaration of an array of function pointers is:
    Code:
    MathFunction trigfunctions[] = {sin, cos, tan, asin, acos};

  3. #3
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    Quote Originally Posted by vlrk View Post
    I am having following pointer variable declaration

    Code:
    Static double (*funcs[]) (double) = { sin,cos,tan,asin,acos};
    Here funcs is an array of pointers to functions which take double as input and gives double as output with static as storage type am I right.
    Looks good to me.
    Fact - Beethoven wrote his first symphony in C

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 05-11-2013, 02:43 PM
  2. Replies: 8
    Last Post: 02-04-2012, 03:40 AM
  3. Replies: 11
    Last Post: 12-30-2009, 04:04 PM
  4. Declaring pointer to variable in struct
    By movl0x1 in forum C Programming
    Replies: 5
    Last Post: 07-09-2007, 11:24 AM
  5. declaring function pointer types for callbacks
    By Pea in forum C Programming
    Replies: 6
    Last Post: 01-06-2005, 09:46 PM