11

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?

2

8 Answers 8

19

If the function you want to supply as the default contents of the array is called func, then

  1. you better use a typedef,
  2. you have to use an array initializer

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 };
Sign up to request clarification or add additional context in comments.

5 Comments

The typedef-less way is wrong. It should be int (*fparr[5])(void) = .... It's not only less readable, but also less writable.
It's freaking easy to get that wrong, look at everybody else on this page ;)
@DanielFischer Sure... :) (Why do you think I suggested that typedef...)
Because, despite being 18 (according to your profile, unless that changed in the last days), you are basically sane.
@DanielFischer (No, that changed on 28 June for the last time) - well, at least I try not to mess up things even more. But sometimes I do.
3

Because you are not actually initialising an array of function pointers ... try:

int (*fparr[5])(void) = { func1, func2, func3, func4, func5 };

Comments

2

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.

2 Comments

@Christoph : In standard C, f() and f(void) are different. () tells the compiler to disable the checking of parameters. for example, if you want to add 1 to a variable i before calling a function f, you can do it so: int f(){;}main(){int i=3;f(i++);printf("%d",i);}.
if you use typedef int (*FN)(); instead of typedef int (*FN)(void); the compiler will happily accept static int can_has_arg(int x) { return x; } FN fparr[] = { can_has_arg } whereas Tom stated that he wants an array of pointers to functions taking no arguments
1

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;
}

2 Comments

EMPTY PARENTHESES ARE NOT EQUIVALENT TO VOID IN C (gcc -Wstrict-prototypes warns: function declaration isn't a prototype
@Jens Thank you for comment. I will fix it.
1

An array of function pointers can be initialized in another way with a default value.


Example Code

   
#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);
}
   
If you run the above program, you will have the below output. All elements are initialized with function add, but 4th element in array is assigned to function sub


Output

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

Comments

1

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;
  }

Comments

0

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;
}

1 Comment

test1 and test2: control reaches end of non-void function. And empty parameter lists should be spelled (void) not () because the latter is old-style (K&R C) and C99-deprecated.
0

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.

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.