arrays - 为什么 strlen() 没有返回正确的值?
问题描述
嗨,我很抱歉这是重复,但没有其他解决方案能够帮助我。变量 found 和 tofind 都是,"Point"
但是当我调用时,strcmp
我得到一个负数,当我打印它们的长度时,find 的长度为 9。有人可以帮我理解为什么会这样以及如何解决(如何让 found 和 tofind具有相同的值和长度)?太感谢了。
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
int main(void) {
char found[] = "";
char tofind[] = "Point";
int i = 0;
while(i < 5){
char f = tofind[i];
strcat(found, &f);
i++;
}
int r = strcmp(found, tofind);
printf("%d\n", r);
printf("Found: %s with len: %lu\n", found, strlen(found));//says len is 9
printf("To Find: %s with len: %lu", tofind, strlen(tofind)); //says len is 5
return 0;
}
输出:
./main
-104
Found: Point with len: 9 this is i: 5
To Find: Point with len: 5
解决方案
推荐阅读
- sql-server - 在 SQL Server 中的例行操作期间在 DB 上搜索
- ansible - Ansible aws_ec2 插件多个私有 ip
- apache - 如何配置本地代理以发送自定义标头请求?
- firebase - 在使用 Firebase 身份验证时,密码是否已经加密?
- sql-server - 在文本 SQL Server 迁移文件中安全地包含敏感值
- .net - Kubernetes 配置:指定不同的服务帐户
- react-native - 在 React Native 中,模态不会旋转到横向屏幕?
- java - Informix JDBC url 可信连接
- android - 如何摆脱 Windows 10 中的“emulator-5580 offline”?
- postgresql - 在考虑冲突键的 PostgreSQL 数据库之间移动数据