Creating function pointers in C with arguments

So I need to write some code that performs partial integration. I have a numerical integrator schematically represented by double integrate(double (*func)(double)); and suppose it works properly, i.e. for a function double f(double x), integrate(f) gives the right result. What I have instead, however, are functions that look like double f(double x, double y); and… Read More Creating function pointers in C with arguments

Invoking function from the 2d array of function pointers

#include <stdio.h> void test1(){ printf("test1\n"); } void test2(){ printf("test2\n"); } void test3(){ printf("test3\n"); } void test4(){ printf("test4\n"); } void (*hv_ptr[2])() = {test1,test2}; void (*dev_ptr[2])() = {test3,test4}; void (**ptr[2])() = {hv_ptr, dev_ptr}; int main() { ptr[0][0]; ptr[0][1]; ptr[1][0]; ptr[1][1]; return 0; } I have declared the array of pointers to function pointers. But with this code,… Read More Invoking function from the 2d array of function pointers

Function pointer assignement and typecast

I wanted to test a my idea about function pointers. I wrote a test but it doesn’t work(causes a Segmentation fault). Here is my test simplified: #include<stdlib.h> void a(){} void b(){} int main(){ size_t s=0; char* p1=(char*)&a; char* p2=(char*)&b; while(*p1++==*p2++) s++; void (*o)(); char* c=malloc(s); while(s–) *(c+s)=*((char*)&a+s); o=(void(*)())c; o(); return 0; } The code should… Read More Function pointer assignement and typecast

How to access the value of a struct member which stores return value of a function pointer?

I have the following structure definitions: typedef struct S_t S_t; struct S_t { float *s_ptr; uint32_t ns; }; typedef struct p_t p_t; struct p_t { int32_t pID; float pVal; }; typedef struct pr_t pr_t; struct pr_t { S_t *S; int (*TrimS)(S_t *TS, int sSize); p_t *TP; }; I also have the following function defined: int… Read More How to access the value of a struct member which stores return value of a function pointer?

Casting function pointer arguments without a helper function

void qsort(void *base, size_t nitems, size_t size, int (*compar)(const void *, const void*)) Is there a way to pass, let’s say strcmp to qsort without making a helper function? I was trying to do: qsort(…, (int (*) (const void*, const void*) (strcmp))); >Solution : Your attempt at the cast simply has a misplaced right (closing)… Read More Casting function pointer arguments without a helper function

How can I register a simple callback function and pass arguments to that function?

What I would like to do is : a simple function "register_function" allowing a user to indicate a callback function to call (that this user has implemented) when calling that function with "call_callback", to be able to pass arguments like a buffer for example Here is my code: static void (*callback)(int *buffer, int size) =… Read More How can I register a simple callback function and pass arguments to that function?

How Should I Define an Array of Pointers to Functions in C++?

I’m trying to make an array of functions so I can call functions from the array with an index. I can’t seem to figure out the parentheses, asterisks and brackets to create this array of functions. Here is what I have: void Game::getPawnMoves(int position, bool color, Move ** moveList) { … } typedef void (*GetMoveFunction)… Read More How Should I Define an Array of Pointers to Functions in C++?