0

Suppose I declare an array of function pointers as follows:

typedef void (*ptr[4])( int a, int b)={fn1,fn2,fn3,fn4};

Now i declare,

ptr *ptr_to_fn_arr;

The question is, how do I call any of the functions( fn1,fn2,fn3,fn4) using the pointer variable ptr_to_fn_arr?

2
  • 1
    Welcome to StackOverflow. A typedef should not contain an assignment. Please provide samples that actually are valid code. And in general please provide a complete sample of your code Commented Jul 12, 2018 at 11:00
  • I agree with @Gerhardh-- to solve your problem, you should separate your typedef with your array allocation. Then you can simply index into the array to access the pointer to your function Commented Jul 12, 2018 at 11:07

2 Answers 2

2

typedef is used to create an alias name for other data type and you cannot initialize a type.
So, this is wrong:

typedef void (*ptr[4])( int a,  int b)={fn1,fn2,fn3,fn4};
                                      ^^^^^^^^^^^^^^^^^

Instead you should do:

typedef void (*ptr[4])( int a,  int b);

This will creates ptr as a type for array of 4 function pointers that takes two int type as argument and does not return any value.

Now using this alias name ptr you can create variable of its type, like this:

ptr x = {fn1, fn2, fn3, fn4};

This will initialize variable x which is an array of 4 function pointers of type ptr.

You can call it like this:

x[0](1, 2); // this is equivalent to fn1(1, 2)
x[1](1, 2); // this is equivalent to fn2(1, 2)
....
.... and so on.

But the statement

ptr x = ...

Just by looking at it, it doesn't seems that x is an array until you look into the ptr typedef.
So, for better readability you can do:

typedef void (*ptr)( int a,  int b);

This will creates ptr as a type for function pointer that takes two int type as argument and does not return any value.

Now to create the array of 4 function pointers of type ptr, you can do:

ptr x[4] = {fn1, fn2, fn3, fn4};

Call it in similar way:

x[2](1, 2); // this is equivalent to fn3(1, 2)
Sign up to request clarification or add additional context in comments.

Comments

1

It'll be easier to do this if we have the typedef be to the function pointer type:

typedef void (*fptr)(int, int);

Now fptr is a typedef for "pointer to function accepting two ints and returning void".

So declaring the array is simple:

fptr fn_arr[4] = {fn1, fn2, fn3, fn4};

And declaring the pointer is simple:

fptr *ptr_to_fn_arr = fn_arr;

But now how do we call the functions?

fn_arr is an array of function pointers, so fn_arr[i] is a function pointer.

ptr_to_fn_arr is a pointer to a function pointers, so *ptr_to_fn_arr is a function pointer. So is ptr_to_fn_arr[i]. (The analogies here to more "ordinary" arrays and pointers should be obvious.)

So the strictly-interpreted way to call via ptr_to_fn_arr would be

(*(*ptr_to_fn_arr))(1, 2)

or

(*ptr_to_fn_arr[i])(3, 4)

But since the only thing you can do with a function pointer is call the pointed-to function, you don't actually need the explicit *, so both of these would work, too:

(*ptr_to_fn_arr)(1, 2)

or

ptr_to_fn_arr[i](3, 4)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.