开发者

Error: conflicting types for ‘strlen’

I have a program, but I get an error message. Please help me on this.

#include <stdio.h>
#include <string.h>
开发者_Go百科int print_strlen(char s[]);

main()
{
    char s[20];
    printf("Enter the string:\n");
    scanf("%s\n", s);
}

int print_strlen(char s[])
{
    int l;
    l = strlen(s);
    printf("Length of the string is: %s\n", l);
}


Don't try to prototype strlen yourself. Just include <string.h>, and use the prototype (and function) it already has. Don't try to write a function of your own with the same name (or, at least officially, any other name starting with str).

The conflicting type it's seeing right now is because the standard requires strlen to return a size_t, not an int.

Also note that the function you have named strlen right now is infinitely recursive -- where it (apparently) tries to call the standard strlen, it'll end up calling itself, and since it does that unconditionally, it'll keep recursing forever (or until the system kills it for overflowing the stack, anyway).


strlen is already defined in string.h hence the error. Change your function name to something else.


Call your function something else, there is already a strlen() declared in string.h.


strlen is a standard function declared in <string.h> and you are trying to define it with another prototype (the standard function takes a char const*, not a char*).


What is your error message?

You shouldn't name your function strlen; there is already a strlen function and you know that won't work because you use the real strlen function in your strlen function. Call it something else, like print_strlen. Also, use void instead of int if you're not actually returning anything from print_strlen.

Other problems not directly related include the obvious buffer overflow; what if you entered text more than 20 bytes long?


strlen is a function provided for you in string.h why reinvent the wheel?

#include <stdio.h>
#include <string.h>
main()
{
        char s[20];
        printf("Enter the string:\n");
        scanf("%s\n",s);
        printf("Length of the string is: %s\n",strlen(s));
}

would work perfectly fine.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜