Reputation: 9643
I want to initialize an array of size 5 pointers that holds pointers to functions that have no parameters and which returns an int (could be any function that facilitate these requirements).
This is what i tried thus far but i get a syntax error:
int (*func)() fparr[5] = int (*func)();
What is wrong with this syntax?
Upvotes: 11
Views: 15325
Reputation: 11
I just add a bit more to the above answers. Array of function pointers can be indexed by an enum variable, showing the type of operation for each index. Take a look at the following example. Here, we use tyepdef for function pointer operator. Then we create an array of this function pointer called act. Finally, we initialize the array to the increment and decrement functions. In this case, index 0 is referred to increment and index 1 is referred to decrement. Instead of using this raw indexing, we use enum which has INCR, and DECR, corresponding to index 0, 1.
#include<stdio.h>
#include<stdlib.h>
typedef void (*operate)(int *, int);
void increment(int *, int);
void decrement(int *, int);
enum {
INCR, DECR
};
int main(void){
int a = 5;
operate act[2] = {increment,decrement};
act[INCR](&a,1);
printf("%d\n",a);
act[DECR](&a,2);
printf("%d\n",a);
return 0;
}
void increment(int *a, int c){
*a += c;
}
void decrement(int *a, int c){
*a -= c;
}
Upvotes: 1
Reputation: 11
An array of function pointers can be initialized in another way with a default value.
#include <stdio.h>
void add(int index, int a, int b){
printf("%d. %d + %d = %d\n", index, a, b, a + b);
}
void sub(int index, int a, int b){
printf("%d. %d - %d = %d\n", index, a, b, a - b);
}
int main(){
void (*func[10])(int, int, int) = {[0 ... 9] = add};
func[4] = sub;
int i;
for(i = 0; i < 10; i++)func[i](i, i + 10, i + 2);
}
0. 10 + 2 = 12
1. 11 + 3 = 14
2. 12 + 4 = 16
3. 13 + 5 = 18
4. 14 - 6 = 8
5. 15 + 7 = 22
6. 16 + 8 = 24
7. 17 + 9 = 26
8. 18 + 10 = 28
9. 19 + 11 = 30
Upvotes: 1
Reputation:
If the function you want to supply as the default contents of the array is called func
, then
typedef
, Consider:
typedef int (*IntFunc)(void);
IntFunc fparr[5] = { func, func, func, func, func };
Or the less readable way, if you prefer to avoid typedef
:
int (*fparr[5])(void) = { func, func, func, func, func };
Upvotes: 19
Reputation: 169573
Example code:
static int foo(void) { return 42; }
int (*bar[5])(void) = { foo, foo, foo, foo, foo };
Note that the types int (*)()
and int (*)(void)
are distinct types - the former denotes a function with a fixed but unspecified number of arguments, whereas the latter denotes a function with no arguments.
Also note that the C declarator syntax follows the same rules as expressions (in particular operator precedence) and is thus read inside-out:
bar
denotes and array (bar[5]
) of pointers (*bar[5]
) to functions (int (*bar[5])(void)
). The parens (*bar[5])
are necessary because postfix function calls bind more tightly than prefix pointer indirection.
Upvotes: 0
Reputation: 15793
Step 1:
define the signature of the functions as a type FN
:
typedef int (*FN)();
Step2:
define the 5 functions with the FN
signature:
int f1(void) { ; }
int f2(void) { ; }
...
Step 3:
define and initialize an array of 5 functions of type FN
:
FN fparr[5] = {f1,f2,f3,f4,f5}
otherwise:
If you do not want to define a separate signature, you can do it -- as said before -- so:
int ((*)fpar []) () = {f1,f2, ...}
If you know the number of functions from the array at the moment of declarations, you do not need to write 5
, the compiler allocated this memory for you, if you initialize the array at the same line as the declaration.
Upvotes: 2
Reputation: 120644
Here is a working example showing the correct syntax:
#include <stdio.h>
int test1(void) {
printf("test1\n");
return 1;
}
int test2(void) {
printf("test2\n");
return 2;
}
int main(int argc, char **argv) {
int (*fparr[2])(void) = { test1, test2 };
fparr[0]();
fparr[1]();
return 0;
}
Upvotes: 0
Reputation: 358
Well, I'm late...
#include <stdio.h>
int fun0()
{
return 0;
}
int fun1()
{
return 1;
}
int fun2()
{
return 2;
}
int main(int argc, char* argv[])
{
int (*f[]) (void) = {fun0, fun1, fun2};
printf("%d\n", f[0]());
printf("%d\n", f[1]());
printf("%d\n", f[2]());
return 0;
}
Upvotes: 1
Reputation: 62323
Because you are not actually initialising an array of function pointers ... try:
int (*fparr[5])(void) = { func1, func2, func3, func4, func5 };
Upvotes: 3