错误:'strlen'的类型冲突

0 c

我有一个程序,但我收到一条错误消息.请帮帮我.

#include <stdio.h>
#include <string.h>
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);
}
Run Code Online (Sandbox Code Playgroud)

Jer*_*fin 5

不要试图strlen自己原型.只需包含<string.h>,并使用它已有的原型(和功能).不要尝试使用相同的名称编写自己的函数(或者,至少在官方,以任何其他名称开头str).

它现在看到的冲突类型是因为标准要求strlen返回a size_t而不是a int.

还要注意,你现在命名的函数strlen是无限递归的 - 它(显然)试图调用标准strlen,它最终会调用自身,并且因为它无条件地执行,它将永远递归(或直到无论如何,系统会杀死堆栈溢出.