arrays - C function pointer neither function nor pointer -


before go on, closest question mine found :subscripted value neither array nor pointer function error yet solution has not applied mine.

i have c function goal take in array of floats , array of functions, apply each function value of same index in float array saving values in array passed in argument.

i careful syntax , defined type function pointers:

typedef float (*func_ptr)(float); 

(and if cares explain im comment or solution why syntax not (does not work as) typedef float (*)(float) func_ptr appreciated)

the function (i think closest functioning) in question looks as:

void vector_function(void * _f, float v[], size_t size, float ret[]){   func_ptr (*f)[size]=_f; //trying cast pointer array of function pointers   float (*f)[size]=_f;   for(size_t i=0; i<size; i++){     ret[i]=f[i](v[i]);   } } 

the error is:

> error: called object not function or function pointer >      ret[i]=f[i](v[i]); >             ^ 

what gets me seems typedef blatantly making f[i] function pointer...

other things have tried are:

void vector_function(void * _f, float v[], size_t size, float ret[]){   float (*f[size])(float)=_f;   for(size_t i=0; i<size; i++){      ret[i]=f[i](v[i]);   } }  void vector_function(float (*f)[size], float v[], size_t size, float ret[]){ //just wrong   for(size_t i=0; i<size; i++){     ret[i]=f[i](v[i]);   } } 

you don't need include size when cast. if void* meant represent array of function pointers, can cast array so:

void vector_function(void * _f, float v[], size_t size, float ret[]){   func_ptr *funcs = (func_ptr*)(_f);   for(size_t i=0; i<size; i++){     func_ptr f = funcs[i];     ret[i]=f(v[i]);   } } 

you don't need indication number of things in array cast, because array pointer hold reference start of array, , there's no information size attached it. requires _f has format, why not make type func_ptr* instead of void*?


Comments

Popular posts from this blog

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -