There is a basic code in the book that implements an example of declaring and using an array of function pointers. When compiling on Windows7 64x in DevC ++, the error is written in "void (* f [3]) (int) = (function1, function2, function3);"

[Warning] left-handed operand

Can you explain why the example does not work?

 #include <stdio.h> #include <stdlib.h> void function1(int); void function2(int); void function3(int); main() { void (*f[3])(int)= (function1, function2, function3); int choice; printf("Enter a number between 0 and 2, 3 to end: "); scanf("%d", &choice); while (choice >= 0 && choice < 3) { (*f[choice])(choice); printf("Enter a number between 0 and 2, 3 to end: "); scanf("%d", &choice); } printf("You entered 3 to end\n"); system("PAUSE"); } void function1(int a) { printf("You entered %d so function1 was called\n\n", a); } void function2(int b) { printf("You entered %d so function2 was called\n\n", b); } void function3(int c) { printf("You entered %d so function3 was called\n\n", c); } 

    1 answer 1

    I do not know what version of the compiler is there, but it worked for me when I rewrote with curly braces.

     void (*f[3])(int)= {function1, function2, function3}; 

    if it does not want to, then you have to write like that

     void (*f[3])(int); f[0] = function1; f[1] = function2; f[2] = function3; 
    • Thank you, I understand that this is possible, but I wanted it as in the example) it’s about the compiler, MinGw is messing up (We need to look at Linux. - Stee1House
    • you need to look at exactly which minGW you have. I compiled gcc 4.7 in Linux (which in fact is the same as mingw). It's also nice to see in the book itself which compiler they recommend. - KoVadim
    • one
      @steelhouse, the book is probably a typo. Correctly with braces . - In parentheses, it turns out just an expression, or rather a sequence of operators. Something a bit unusual (but quite understandable): int a, b, c = (a = 1, b = ++ a, a + b); printf ("(2 + 2 = 4) a =% db =% dc =% d \ n", a, b, c); In your case, the result of the expression (function1, function2, function3) will simply be the address of function3 (as the last operator in the list), which is not suitable (at least with a compiler) for initializing an array of 3 function addresses. - avp 8:38 pm