The code states:
void (* log_msg)(char *msg)
=printf;
void change_and_log(int *buffer, int offset, int value){
buffer[offset] = value;
log_msg("changed");
}
I’m most concerned with the first part:
Firstly, what does the signature void (* log_msg)(char *msg) mean? Is this code simply mapping the function log_msg to printf? In that case, why is the function name (* log_msg) and not simply log_msg?
It’s a function pointer.
The type of a function pointer is
R (*)(Args...), whereRandArgs...are replaced with the return type and arguments, if any. It is read as “a pointer to a function that takes argumentsArgs...and returnsR.”Your code would read easier as:
And later, it’s just calling that function via a function pointer.