Following is the excerpt from the program: I have tried to re-produce as little code as possible to focus on the warning message.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/* function declarations */
void userwritelines(char *[],int );
int usernumcmp(char *, char *);
/* constants */
#define MAXLINES 10
#define MAXWIDTHPERLINE 10
void userqsort(void *lineptr[], int left, int right, int (*comp)(void *, void *));
char *lineptr[MAXLINES] = { "testing", "whether", "it would", "work"};
int main()
{
int nlines, numeric;
nlines = 4;
numeric = 0;
userqsort((void **) lineptr, 0, nlines -1, (int (*)(void *, void *)) (numeric ? usernumcmp : strcmp));
userwritelines(lineptr, nlines);
return 0;
}
/* qsort: sort v[left]...v[right] into increasing order */
void userqsort(void *v[], int left, int right,
int (*comp)(void *, void *))
{
int i, last;
void swap(void *v[], int, int);
if(left >= right) /* do nothing if array contains */
return; /* fewer than 2 elements */
swap(v, left, (left+right)/2);
last = left;
for (i = left+1; i <= right; i++)
if((*comp)(v[i],v[left]) < 0)
swap(v, ++last, i);
swap(v, left, last);
userqsort(v, left, last-1, comp);
userqsort(v, last+1, right, comp);
}
void swap(void *v[],int i, int j)
{
void *temp;
temp = v[i];
v[i] = v[j];
v[j] = temp;
}
/* writeline function to write the strings as pointed by lineptr i.e. pointer of arrays to char */
void userwritelines(char *lineptr[],int countofline)
{
while(countofline > 0){
printf("%s\n",*lineptr);
lineptr++;
countofline--;
}
}
/* usernumcmp: compare s1 and s2 numerically */
int usernumcmp(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 compiled, it generates the following warning:
51009396@NHQ-GF-51009396 /cygdrive/d/Let us C/Practice
$ gcc -g -Wall sopointertofunc.c -o sopointertofunc.exe
sopointertofunc.c: In function ‘main’:
sopointertofunc.c:21:100: warning: pointer type mismatch in conditional expression
15 | userqsort((void **) lineptr, 0, nlines -1, (int (*)(void *, void *)) (numeric ? usernumcmp : strcmp));
^
Can you explain why this warning is getting generated in the first place? While i know the moment i would add const
keyword to function prototype and definition of usernumcmp function, the warning disappears, but why does the warning appears in the first place?
It just means that while computing the following expression:
numeric ? usernumcmp : strcmp
usernumcmp
and strcmp
have different pointer type. You have not shown usernumcmp
but I would assume int usernumcmp(int *, int*)
or something close, while strcmp
is int strcmp(const char*, const char*)
.
It does not matter much, because you immediately cast the result to int (*)(void *, void *)
but if you want to get rid of the warning, you should cast the functions before computing the expression.
typedef int (*compfunc)(void*, void*);
...
userqsort((void**)lineptr, left, right, (numeric ? (cmpfunc) usernumcmp : (cmpfunc)strcmp));