...
| Wiki Markup |
|---|
In this non-compliant code example, the function pointer {{fn_ptr}} refers to the function {{add()}}, which accepts three integer arguments. However, {{fn_ptr}} is specified to accept two integer arguments. Setting {{fn_ptr}} to refer to {{add()}} results in an unexpected program behavior. This example also violates rule \[[DCL35-C. Do not convert a function pointer to a function of an incompatible type]\]. |
| Code Block | ||
|---|---|---|
| ||
int add(int x, int y, int z) {
return x + y + z;
}
int main(int argc, char *argv[]) {
int (*fn_ptr) (int, int);
int res;
fn_ptr = add;
res = fn_ptr(2, 3); /* incorrect */
/* ... */
return 0;
}
|
...