I wrote this simple code for string comparison
#include<stdio.h>
void strCmp(char *,char *);
int main() {
char* str1 = "hello";
char* str2 = "hello";
strCmp(str1,str2);
return 0;
}
void strCmp(char *vp1,char *vp2) {
int r = strcmp(vp1,vp2);
printf("result %d",r);
}
And then to check what will happen if I pass void* instead of char*, I rewrote the code like this:
#include<stdio.h>
void strCmp(void *,void *);
int main() {
char* str1 = "hello";
char* str2 = "hello";
strCmp(str1,str2);
return 0;
}
void strCmp(void *vp1,void *vp2) {
int r = strcmp(vp1,vp2);
printf("result %d",r);
}
The above code gets compiled and works as expected! If I go crazy further and do
#include<stdio.h>
void strCmp(void **,void **);
int main() {
char** str1 = "helloxx";
char** str2 = "hellox";
strCmp(str1,str2);
return 0;
}
void strCmp(void **vp1,void **vp2) {
int r = strcmp(vp1,vp2);
printf("result %d",r);
}
No failure and the results are fine. I wondered what is the implementation of strcmp and copy pasted the code from https://stackoverflow.com/a/10927187/2304258 as a custom function in my code but it fails with the usual error messages you expect when you use a void*
without casting.
Can you please explain how the above implementations are working?