gpt4 book ai didi

时间:2019-03-17 标签:c printf

转载 作者:行者123 更新时间:2023-11-30 20:43:54 24 4
gpt4 key购买 nike

我的代码可以编译,但 printf 没有显示任何内容?

如果我取出 printf 的格式化程序部分,那么它就可以正常工作。

#include <stdio.h>

size_t MyStrlen(const char *s1)
{
const char *s1Copy = s1;

while (*s1)
{
*s1Copy++;
}

return s1Copy -s1;
}

int main(void)
{
const char str[] = "HELLO";

printf("Length: %d \n", (unsigned int)MyStrlen(str));

return 0;
}

最佳答案

在循环测试中,您想要测试 *s1Copy 是否为非 NULL,而不是 *s1,您不会递增。事实上,由于 *s1 永远不会改变,因此您将使用 s1Copy++ 直接从字符串参数的末尾走过去,并且代码不会正常终止。

当然,除非您传递空字符串:您的 MyStrlen 方法适用于空字符串。

while (*s1Copy)
s1Copy++;

关于时间:2019-03-17 标签:c printf,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11623749/

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