ANSI C strstr() 不使用指针
ANSI C strstr() not working with pointers
我正在尝试使用 strstr() 在字符串中查找子字符串。仅使用 char[] 有效,char* 无效,出现分段错误。
所以这个有效:
int main() {
char str[] = "apple apple peach";
char *p;
p = strstr(str, "peach");
if (p!= NULL) {
strncpy(p, "apple", 5);
}
return 0;
}
但是这个不起作用:
int main() {
char *str = "apple apple peach";
char *p;
p = strstr(str, "peach");
if (p!= NULL) {
strncpy(p, "apple", 5);
}
return 0;
}
这一个都不是:
int main() {
char *str = "apple apple peach";
char *peach = "peach";
char *p;
p = strstr(str, peach);
if (p!= NULL) {
strncpy(p, "apple", 5);
}
return 0;
}
这一个都不是:
int main() {
char *str = "apple apple peach";
char peach[] = "peach";
char *p;
p = strstr(str, peach);
if (p!= NULL) {
strncpy(p, "apple", 5);
}
return 0;
}
这是已知错误或功能吗?
您的问题 不是 strstr
,而是 strncpy
。在指针的情况下,您正在使用 strncpy
尝试写入 字符串文字 ,这是常量。
与strstr
无关:它按预期工作。当您尝试 修改 分配给字符串文字的内存时,崩溃发生在 strcpy
中。这是未定义的行为。
解决这个问题的关键是将数据放入允许写入的内存中。您的第一个程序通过声明 str
数组来做到这一点:
char str[] = "apple apple peach";
那不是UB,所以程序运行完成。另一种选择是在动态内存中分配 str
,如下所示:
char *str = malloc(20);
strcpy(str, "apple apple peach");
char *p;
p = strstr(str, "peach");
if (p!= NULL) {
strncpy(p, "apple", 5);
}
free(str);
string-literal 修改不安全! char[]
成功只是因为 strncpy(p, "apple", 5);
修改了 string-literal 的复制版本, 而不是 string-literal 本身 。标准中的相关段落如下:
6.4.5, paragraph 6
... The multibyte character sequence is then used to initialize an array of static storage duration and length just sufficient to contain the sequence...
6.4.5, paragraph 7
... If the program attempts to modify such an array, the behavior is undefined.
简单来说,string-literal就是(概念上)换成了数组,修改这个不安全。请注意 string-literal 不一定是 const 限定的,尽管 MSVC++ 似乎强制要求 string-literal 是 const char *
或其他东西const 限定。
我正在尝试使用 strstr() 在字符串中查找子字符串。仅使用 char[] 有效,char* 无效,出现分段错误。
所以这个有效:
int main() {
char str[] = "apple apple peach";
char *p;
p = strstr(str, "peach");
if (p!= NULL) {
strncpy(p, "apple", 5);
}
return 0;
}
但是这个不起作用:
int main() {
char *str = "apple apple peach";
char *p;
p = strstr(str, "peach");
if (p!= NULL) {
strncpy(p, "apple", 5);
}
return 0;
}
这一个都不是:
int main() {
char *str = "apple apple peach";
char *peach = "peach";
char *p;
p = strstr(str, peach);
if (p!= NULL) {
strncpy(p, "apple", 5);
}
return 0;
}
这一个都不是:
int main() {
char *str = "apple apple peach";
char peach[] = "peach";
char *p;
p = strstr(str, peach);
if (p!= NULL) {
strncpy(p, "apple", 5);
}
return 0;
}
这是已知错误或功能吗?
您的问题 不是 strstr
,而是 strncpy
。在指针的情况下,您正在使用 strncpy
尝试写入 字符串文字 ,这是常量。
与strstr
无关:它按预期工作。当您尝试 修改 分配给字符串文字的内存时,崩溃发生在 strcpy
中。这是未定义的行为。
解决这个问题的关键是将数据放入允许写入的内存中。您的第一个程序通过声明 str
数组来做到这一点:
char str[] = "apple apple peach";
那不是UB,所以程序运行完成。另一种选择是在动态内存中分配 str
,如下所示:
char *str = malloc(20);
strcpy(str, "apple apple peach");
char *p;
p = strstr(str, "peach");
if (p!= NULL) {
strncpy(p, "apple", 5);
}
free(str);
string-literal 修改不安全! char[]
成功只是因为 strncpy(p, "apple", 5);
修改了 string-literal 的复制版本, 而不是 string-literal 本身 。标准中的相关段落如下:
6.4.5, paragraph 6
... The multibyte character sequence is then used to initialize an array of static storage duration and length just sufficient to contain the sequence...
6.4.5, paragraph 7
... If the program attempts to modify such an array, the behavior is undefined.
简单来说,string-literal就是(概念上)换成了数组,修改这个不安全。请注意 string-literal 不一定是 const 限定的,尽管 MSVC++ 似乎强制要求 string-literal 是 const char *
或其他东西const 限定。