Home > Mobile >  Can I assign a function pointer to a function with const parameters in C ?
Can I assign a function pointer to a function with const parameters in C ?

Time:11-08

If i have a funtion int foo(const int A), am I allowed to do the following coding?

int (*Mypointer)(const int);
Mypointer = &foo;

In addition, am i also allowed to do the following which is without const in the declaration of the function pointer ?

int (*Mypointer)(int);
Mypointer = &foo;

Another interesting case is that, if I have int foo(int A) and I declare that

int(*Mypointer)(const int);
Mypointer = &foo;

I was given segmentation fault

CodePudding user response:

They should both work.

const applied to a parameter passed by value has an effect ONLY in function body, making it immutable there.

  • Related