c - Array of constant pointers to functions -
i want make array of constant pointers functions. this:
#include <stdio.h> #include <stdlib.h> int f( int x); int g( int x ); const int ( *pf[ ] )( int x ) = { f, g }; int main(void) { int i, x = 4, nf = 2; for( = 0; < nf; i++ ) printf( "pf[ %d ]( %d ) = %d \n", i, x, pf[ ]( x ) ); return exit_success; } int f( int x ) { return x; } int g( int x ) { return 2*x; }
it works "fine" when compiled without -werror flag, otherwise get:
building file: ../src/probando.c invoking: gcc c compiler gcc -o0 -g3 -pedantic -pedantic-errors -wall -wextra -werror -wconversion -c -fmessage-length=0 -mmd -mp -mf"src/probando.d" -mt"src/probando.d" -o "src/probando.o" "../src/probando.c" ../src/probando.c:17:14: error: type qualifiers ignored on function return type [-werror=ignored-qualifiers] ../src/probando.c:17:1: error: initialization incompatible pointer type ../src/probando.c:17:1: error: (near initialization ‘pf[0]’) ../src/probando.c:18:1: error: initialization incompatible pointer type ../src/probando.c:18:1: error: (near initialization ‘pf[1]’) cc1: warnings being treated errors make: *** [src/probando.o] error 1
thanks in advance.
the const
misplaced. stands, functions supposed return const int
(which makes little sense). want is:
int (*const x[])(int)
this way reads: array of "const pointers function".
Comments
Post a Comment