#include<stdlib.h>
#include<string.h>
#include<stdio.h>
int pstrcmp( char **p,char **q)
{ return strcmp(*p,*q) ;
}
int comlen(char *p,char *q)
{
int i=0;
while(*p && (*p++=*q++))
i++;
return i;
}
#define M 1
#define MAXN 5000000
char c[MAXN],*a[MAXN];
int main()
{
int i,ch,n=0,maxi,maxlen=-1;
while((ch=getchar())!=EOF){
a[n]=&c[n];
c[n++]=ch;
}
c[n]=0;
qsort(a,n,sizeof(char *),pstrcmp);
for(i=0;i<n-M;i++)
if(comlen(a[i],a[i+M])>maxlen){
maxlen=comlen(a[i],a[i+M]);
maxi=i;
}
printf("%.*s\n",maxlen,a[maxi]);
return 0;
}
in this code compiler shows me error
Error 1 error C2664: 'qsort' : cannot convert parameter 4 from 'int (__cdecl *)(char **,char **)' to 'int (__cdecl *)(const void *,const void *)' d:\fe\longest_duplicated\longest_duplicated\longest_duplicated.cpp 33 longest_duplicated
i know that,have to convert from void type to char type,but how to do it dont know and please help me
Change your function to
and the error should be gone.
qsortexpects aint(*compar)(const void *, const void *)as 4th parameter while you are passing it a function that takes twochar**arguments.P.S.: I did just analyse the error message, not your program, since it is so badly formatted. Maybe improving the formatting would help here.
UPDATE: You are not creating strings, you are storing pointers in
athat point into some location inc. A string needs to be terminated with a0in order to work. Although the whole design of your program looks strange, you could do