Comments (3)
有没有测试的case呢?具体传入什么字符串会导致返回0呢?
from hurlex-doc.
如果两个字符串的长度和字符都相等才会返回0。比如字符串str1="test a"
和字符串str2="test b"
,按照之前的源码当判断到两个字符串最后的字符a
和b
时,由于不相等会出while
循环,但是代码中进行了++
操作,就直接到达两个字符串的结束符位置了,然后会进if(*str1 == '\0' && *str2 == '\0'
这个条件,返回0,但是其实这两个字符串虽然长度相同但并不相等。
测试代码(调用了库中的strcmp
做比较):
#include <stdio.h>
#include <string.h>
int my_strcmp(const char *str1, const char *str2)
{
while (*str1 && *str2 && (*str1++ == *str2++))
;
if (*str1 == '\0' && *str2 == '\0') {
return 0;
}
if (*str1 == '\0') {
return -1;
}
return 1;
}
int main()
{
char *str1 = "test a";
char *str2 = "test b";
printf("%d\n", my_strcmp(str1, str2));
printf("%d\n", strcmp(str1, str2));
return 0;
}
from hurlex-doc.
嗯, 是的,这个函数是有问题的。多谢指出问题,已经修复并提交。
同时字符串函数移动到头文件里了(inline函数放在.c里好奇怪,不知道当时从哪里来的代码)。
from hurlex-doc.
Related Issues (20)
- Error 13: Invalid or unsupported executable format HOT 1
- 关于printk函数的bug
- 從實模式進入保護模式 HOT 1
- ubuntu18.10bug HOT 8
- 第10章的程序无法正确执行 HOT 1
- vmware无法启动
- 《第十章》在GCC高版本无法运行,解决方法麻烦更新到文档中,谢谢。 HOT 2
- 是不是还未完成? HOT 4
- 请问 使用的qemu 是什么版本呀 HOT 2
- 第五章 文章描述的一点疑惑 HOT 2
- 在线版文档的图片挂了 HOT 1
- heap.c中split_chunk()函数中指针操作的疑问 HOT 2
- enadle_intr和disable_intr链接未定义 HOT 1
- “八,完成中断请求和定时器中断”捕获不到键盘的中断? HOT 4
- 安装好了GRUB的软盘镜像在哪里 HOT 1
- 关于chap3修改了entry.c后并无变化 HOT 1
- 请问为什么设置完GDT之后 只更新了寄存器 却没有设置cr0进入保护模式? HOT 1
- 文档第七页,地址 HOT 1
- 文档18页 HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from hurlex-doc.