我有 memmove 的问题。我通过可视化器运行这段代码,我得到一个 SIGSEGV 错误。
int main() {
char *src;
src = "this is a message, this is a new message";
memmove(src, &(src[18]), 21);
return 0;
}
我有 memmove 的问题。我通过可视化器运行这段代码,我得到一个 SIGSEGV 错误。
int main() {
char *src;
src = "this is a message, this is a new message";
memmove(src, &(src[18]), 21);
return 0;
}
src是指向由字符串文字创建的字符串的指针。你不能修改它。您可以修改数组的内容,因此创建一个包含字符串的数组,如下所示:
char src[] = "this is a message, this is a new message";
此外,假设您想以 结尾this is a new message,那么您开始过早(src[18]是之前的空格this),并且您过早停止了两个(您不包括 finale或字符串终止的 NUL)。
1 2 3 4 first to include = 19
01234567890123456789012345678901234567890 first to exclude = 41
this is a message, this is a new message␀ length = 41 - 19 = 22
因此,您应该使用以下内容:
memmove(src, &(src[19]), 22);
或者
memmove(src, src+19, 22);