c++ - pointer to function wont work -
i'm trying learn how use pointer function properly. supposed make pointer function strcmp
, inside function check
, program opens , closes immediately. far understood, pointer function correct on code returntype (*pointer)(parameters));
going wrong? in advance.
void check(char *a,char *b,int (*cmp)(const char*,const char*)) { printf("testing equality\n"); if(!(*cmp)(a,b)) printf("equals"); else printf("different"); } int main(void) { char s1[80] = "daniel" ,s2[80] = "daniel"; int (*p)(const char*,const char*); p = strcmp(); check(s1,s2,p); return 0; }
this line incorrect:
p = strcmp();
here you're calling strcmp
0 arguments, invalid. should have gotten clear compiler error this. instance, gcc gives me:
error: few arguments function ‘
int strcmp(const char*, const char*)
’
you want assign strcmp
:
p = strcmp;
also, don't need dereference function pointers call them, !(*cmp)(a,b)
simplified !cmp(a,b)
.
Comments
Post a Comment