C 语言strcpy函数的一个问题?
本文介绍了C 语言strcpy函数的一个问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
问 题
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/*
* make sure all paths are below the current directory
*/
void sanitize(char *str)
{
char *src, *dest;
src = dest = str;
while (*src) {
if (strncmp(src, "/../", 4) == 0) {
src += 3;
} else if (strncmp(src, "//", 2) == 0) {
src ++;
} else {
*dest++ = *src++;
}
}
*dest = '\0';
if (*str == '/')
strcpy(str, str + 1);
if (str[0] == '\0' || strcmp(str, "./") == 0
|| strcmp(str, "./..") == 0) {
strcpy(str, ".");
}
}
int main() {
char buf[] = "/../status";
sanitize(buf);
printf("%s\n", buf);
}
上面的代码在执行strcpy(str, str + 1);
的时候报错了 什么情况?
$ ./sanitize.out
[1] 19007 abort ./sanitize.out
解决方案
原型声明:char strcpy(char dest, const char *src);
头文件:#include <string.h> 和 #include <stdio.h>
功能:把从src地址开始且含有NULL结束符的字符串复制到以dest开始的地址空间
说明:src和dest所指内存区域不可以重叠且dest必须有足够的空间来容纳src的字符串。
注意 : src和dest不能重叠。
这篇关于C 语言strcpy函数的一个问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文