...
In this example, the function pointer fp
is used to refer to the function strchr()
. However, fp
is declared without a function prototype. As a result, there is no type checking performed on the call to fp(12,2);
. A typedef is included for compatibility with DCL05-C. Use typedefs to improve code readability.
Code Block | ||
---|---|---|
| ||
#include <stdio.h> #include <string.h> typedef char *(*fpnoarg_fn) (); noarg_fn fp; int main(void) { char *c; fp = strchr; c = fp(12, 2); printf("%s\n", c); return 0; } |
Compliant Solution (Function Pointers)
...
Code Block | ||
---|---|---|
| ||
#include <string.h> typedef char *(*fptwoarg_fn) (const char *, int); twoarg_fn fp; int main(void) { char *c; fp = strchr; c = fp("Hello",'H'); printf("%s\n", c); return 0; } |
Noncompliant Code Example (Variadic Functions)
...