1

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.

AviouslyAK
  • 107
  • 5
  • `int *fun;` That should rather be `int (*fun)(const char*, const char*)` because you set it to point to a function, not an integer. – dxiv Jun 04 '20 at 03:04
  • 3
    "numcmp and strcmp are already of type `int`" - what makes you think that? – kmdreko Jun 04 '20 at 03:07
  • I defined numcmp to be an int and the man page for strcmp says it returns an int – AviouslyAK Jun 04 '20 at 03:09
  • 1
    "I can fix this by casting numcmp and strcmp to (int*), " - then how are you going to call the function? – M.M Jun 04 '20 at 03:13
  • 2
    @AviouslyAK `the man page for strcmp says it returns an int` A function is not the same thing as its return type. Lookup function pointers, for a start [How do function pointers in C work?](https://stackoverflow.com/questions/840501/how-do-function-pointers-in-c-work). – dxiv Jun 04 '20 at 03:15
  • @dxiv ```fun = numeric ? numcmp : strcmp``` assigns fun to the address of either numcmp or strcmp, to be called later. Why does that cause a pointer mismatch though? – AviouslyAK Jun 04 '20 at 03:22
  • @AviouslyAK Because your `fun` is declared to be the wrong (and incompatible) type, see my first comment. Once you fix that, you'll then need to recheck `numcmp` and make sure it has the same prototype as `strcmp`. – dxiv Jun 04 '20 at 03:25
  • 2
    @dxiv Thank you! Adding const to the parameters of numcmp fixed the issue (and then fixing the declaration of fun fixed the new issue). – AviouslyAK Jun 04 '20 at 03:31

1 Answers1

2

In your main() function, in this line fun = numeric ? numcmp : strcmp; you think you are assigning the address of either strcmp or numcmp according to the condition. But that's not how its done in C. Grab your attention on the code given below:

#include <stdio.h>

double mul(int a, int b)
{
    return (double)(a*b);
}

int main()
{
    double (*p)(int, int);
    p = mul;
    int a = 10, b = 10;
    printf("%Lf", (*p)(10, 10));
    return 0;
}

If you notice, how the pointer p is declared and how the address of mul assigned to it. That's how we assign the address of a function to a function pointer. So, as dxiv suggested you in his comment and according to your code, numeric ? numcmp : strcmp; will always return strcmp and if you know about the syntax of strcmp, its

int strcmp(const char*, const char*);

so you should declare fun as

int (* fun)(const char*, const char*);

and then assign

fun = numeric ? numcmp : strcmp;

to it. Also change the arguments of numcmp from char * to const char*.

Hope, this answer explains why you are getting an incompatibility error.

Shubham
  • 1,153
  • 8
  • 20