If I write something as follows:
int f(char x, const int (*g) (const char x)) {
return g(x);
}
does the first const say effectively what I think it says, namely basically that the programmer can't change the code of the function g? So basically, it is a good practice to put those consts when passing a function pointer as a parameter?
The first
constdeclares the returnedintto be immutable - but that will not matter since the receiver will copy it into a non-constintif that's wanted - which is exactly what yourf()function does. It callsg()which is declared to return aconst intbutf()returns anint. The firstconstwill just be ignored.It does not have to do with changing the code of
g(). That's not allowed under any circumstances.If you want to make it clear that the function pointer will not be made to point at another function inside
f: