The code should decide which function to use (strcmp or numcmp) based off the value of numeric. It will always result in strcmp but that is irrelevant as the error occurs regardless of the value of numeric
.
#include <string.h>
#include <stdlib.h>
int numcmp(char *, char *);
int main(int argc, char *argv[]) {
int numeric = 0;
int *fun;
fun = numeric ? numcmp : strcmp;
return 0;
}
int numcmp(char *s1, char *s2) {
double v1, v2;
v1 = atof(s1);
v2 = atof(s2);
if (v1 < v2)
return -1;
else if (v1 > v2)
return 1;
else
return 0;
}
When compiling with gcc --std=c89 test.c
it results in the error:
test.c: In function ‘main’:
test.c:9:28: warning: pointer type mismatch in conditional expression
9 | fun = numeric ? numcmp : strcmp;
|
I can fix this by casting numcmp and strcmp to (int*)
, but this should be irrelevant as numcmp and strcmp are already of type int
.