1.strcmy
函数定义:int strcmy(const char* str1, const char* str2)
返回值:若字符串str1> str2, 返回值>0; 若字符串str1==str2, 返回值=0; 若字符串str1 < str2, 返回值 <0
存在的问题:对入参的检测
例如:
<strong><span style="color:#ff0000;">[wln@localhost c]$ g++ -o strcmp strcmp.c
[wln@localhost c]$ ./strcmp </span></strong>
<span style="color:#ff0000;"><strong>段错误 (core dumped)</strong></span>
[wln@localhost c]$ cat strcmp.c
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
#include <string.h>
/*
* 使用strcmy看不验证条件是否存在问题
*/
int main(void)
{
const char *str1=NULL;
const char *str2="hello";
//判断字符串是否为NULL
#if 0
assert (str1 != NULL);
assert (str2 != NULL);
#endif
int result=strcmp(str1,str2);
if (result<0)
{
printf("str1 < str2\n");
}
else if(result==0)
{
printf("str1 == str2\n");
}
else
{
printf("str1 > str2\n");
}
}
还有个函数:strncmp函数
int strncmp(const char *s1, const char *s2, size_t n);
个人认为用strncmp 不用strcmp原因是:
(1)本来只需要比较前n位
(2)有可能字符串结束符不同,比如:一个'\n',一个'\r'\n'
...待总结
2. 函数strcpy
入参检测