c, function pointer point to different function

Thread Starter

bug13

Joined Feb 13, 2012
2,002
Hi guys

Is there a way to make a function pointer to point to different function?? For example:
Code:
// function pointer to a function with no return and no parameter
typedef void (* func_1) (void);

// function pointer to a function with int return, and int parameter
type int (*func_2)(int);
Is there a way to make a function pointer that can point to both?
Code:
// some kind of function pointer that can point to both
typedef [NULL??] (*func_all)(NULL??);

func_all func_void;
func_all func_int;

func_void = func_1;
func_int = func_2;
Thanks guys!
 

Ian Rogers

Joined Dec 12, 2012
1,136
Use a pointer array... each element can point to a different function... Menu systems sometimes use this method... As to your other question.. OpenRTOS has available C code...
 

jfitter

Joined Mar 14, 2019
14
Hi guys

Is there a way to make a function pointer to point to different function?? For example:
Code:
// function pointer to a function with no return and no parameter
typedef void (* func_1) (void);

// function pointer to a function with int return, and int parameter
type int (*func_2)(int);
Is there a way to make a function pointer that can point to both?
Code:
// some kind of function pointer that can point to both
typedef [NULL??] (*func_all)(NULL??);

func_all func_void;
func_all func_int;

func_void = func_1;
func_int = func_2;
Thanks guys!
The short answer is no. Function overloading is not a feature of C.
1. Recode in C++
2. Have a read if this https://stackoverflow.com/questions/2873850/is-there-an-equivalent-in-c-for-c-templates. It offers some workarounds for C, but they are pretty ugly.
 

MrSoftware

Joined Oct 29, 2013
2,273
This is a bad idea, type checking is one of the few things in C that can help save you from silly errors. That said; with casting you can do pretty much whatever you want. Such as:

Code:
typedef void(*funcPtr) (void* funcParam);

void myIntFunc(void* x)
{
    // First cast the data ty pe
    int* pMyVar = (int*)x;
    printf("My int func: %i\n\n", *pMyVar);
}

void myVoidFunc(void* x)
{
    printf("My void func\n\n");
}

void myStringFunc(void* x)
{
    // Cast to get our data
    char* pStr = (char*)x;
    printf("myStringFunc: %s\n\n", pStr);

}


int main()
{
    funcPtr myFunc = (funcPtr)(&myVoidFunc);
    myFunc(NULL);
  
    int myInt = 10;
    myFunc = (funcPtr)(&myIntFunc);
    myFunc(&myInt);

    char myStr[] = "Hello world!";
    myFunc = (funcPtr)(&myStringFunc);
    myFunc(myStr);
  
}
upload_2019-3-18_8-17-36.png
 

Attachments

Thread Starter

bug13

Joined Feb 13, 2012
2,002
This is a bad idea, type checking is one of the few things in C that can help save you from silly errors. That said; with casting you can do pretty much whatever you want. Such as:

Code:
typedef void(*funcPtr) (void* funcParam);

void myIntFunc(void* x)
{
    // First cast the data ty pe
    int* pMyVar = (int*)x;
    printf("My int func: %i\n\n", *pMyVar);
}

void myVoidFunc(void* x)
{
    printf("My void func\n\n");
}

void myStringFunc(void* x)
{
    // Cast to get our data
    char* pStr = (char*)x;
    printf("myStringFunc: %s\n\n", pStr);

}


int main()
{
    funcPtr myFunc = (funcPtr)(&myVoidFunc);
    myFunc(NULL);
 
    int myInt = 10;
    myFunc = (funcPtr)(&myIntFunc);
    myFunc(&myInt);

    char myStr[] = "Hello world!";
    myFunc = (funcPtr)(&myStringFunc);
    myFunc(myStr);
 
}
View attachment 172645

I can see this is a bad idea, at the same time I think it’s also quite cool. You can do a lot with void pointer.
 

Ian Rogers

Joined Dec 12, 2012
1,136
Yes! But its no different to just calling three different functions.. All this is good stuff, but the idea is you call a function depending on an event.
So a switch statement or an array will still have several functions... Even with the automation of C++ with overloading, there are separate functions.... bug13 asked if you could make a function pointer point to different functions... The answer is Yes but one function at a time using any of the above answers.. A single pointer can only point to one location at a time.. You will still need to decipher which to point to..
 
Top