i am trying to create a function like strlen() in string.h
It’s giving me the error can not convert char* to char
#include<stdio.h>
#include<conio.h>
int xstrlen(char string);
void main(void) {
char string[40];
puts("Enter string:");
gets(string);
printf(" %s is the length of %d", string, xstrlen(string));
}
int xstrlen(char string[]) {
int i;
for (i=0; ; i++) {
if (string[i] == '\0')
break;
}// for ends
return i;
}
The
xstrlenprototype says that the function takes just a single char. That’s what the compiler knows when compiling your main function – and hence it complains, because you’re passing an array to it.Note that the xstrlen definition itself has the correct signature (taking an array).
Just fix the xstrlen prototype to read