gpt4 book ai didi

c - 即使在 C 中使用#include ,strlen 也不起作用

转载 作者:行者123 更新时间:2023-11-30 21:35:45 24 4
gpt4 key购买 nike

它不返回一个 int 或其他什么吗?这是我的代码片段:

int wordlength(char *x);

int main()
{
char word;
printf("Enter a word: \n");
scanf("%c \n", &word);
printf("Word Length: %d", wordlength(word));
return 0;
}

int wordlength(char *x)
{
int length = strlen(x);
return length;
}

最佳答案

函数strlen应用于以零结尾的字符串(字符数组)。您正在将该函数应用于指向单个字符的指针。因此该程序具有未定义的行为。

关于c - 即使在 C 中使用#include <string.h>,strlen 也不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26330229/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com