不能创建一个函数来切割 char 数组的一部分 (C99)
Can't make a function that cut a part of a char array (C99)
我正在尝试创建一个函数来剪切字符串的一部分,以便在所需的位置和所需的长度删除该部分。
我收到 SIGSEGV 错误(分段错误),而 运行 "Str[Pos] = 0" 的调试器。我不明白为什么,因为我只是想将这个特定位置的 char 指针设置为 0 或 '\0',这样它就好像它是数组的末尾一样。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char **argv)
{
char * String = malloc(512);
String = "Blobfish is the best creature ever made by nature";
CutString(String, 3, 4);
print("%s", String);
}
bool CutString(char * Str, int Pos, int Size)
{
int StrLen = strlen(Str);
printf("After4\n");
if (Size < 1 || Size + Pos > StrLen) return true;
printf("After4.1\n");
char * StrPos = Str + Pos + Size;
printf("After4.2\n");
printf("%s", StrPos);
Str[Pos] = 0;
printf("After4.3\n");
strcat(Str, StrPos);
printf("After4.4\n");
return false;
}
在此先感谢您的帮助和保重,
朱尔斯
代码正在尝试更改字符串文字,这是未定义的行为。
改为修改分配的内存。
char * String = malloc(512);
// The below only copies the pointer to the string literal, not the string contents.
// String = "Blobfish is the best creature ever made by nature";
strcpy(String, "Blobfish is the best creature ever made by nature");
该函数无效,但足以表明该程序在任何情况下都有未定义的行为,因为它试图修改字符串文字,而修改字符串文字会导致未定义的行为。
char * String = malloc(512);
String = "Blobfish is the best creature ever made by nature";
CutString(String, 3, 4);
//...
此外还有内存泄漏,因为首先分配了内存并将指向内存的指针分配给了指针String。然后指针 String 被重新分配了一个字符串文字的地址。所以分配内存的地址丢失了。
至于函数则可以按如下方式定义,如下面的演示程序所示。
#include <stdio.h>
#include <string.h>
char * CutString( char * s, size_t pos, size_t n )
{
size_t length = strlen( s );
if ( pos < length )
{
n = length - pos < n ? length - pos : n;
memmove( s + pos, s + pos + n, length - pos - n + 1 );
}
return s;
}
int main(void)
{
char s[] = "Blobfish is the best creature ever made by nature";
puts( s );
puts( CutString( s, 3, 4 ) );
return 0;
}
程序输出为
Blobfish is the best creature ever made by nature
Bloh is the best creature ever made by nature
注意函数的return类型bool没有什么意义。当这样的函数 returns 指向修改后的字符串本身时会好得多。所有标准 C 字符串函数都遵循此约定。
我不太清楚你想用这个函数做什么。但我强调了几个问题:
这会给您带来麻烦。因为 Size + Pos 应该总是大于 StrLen。因为 Strlen 应该等于大小。
if (Size < 1 || Size + Pos > StrLen) return true;
改写为:
if (Size < 1 || Pos > StrLen)
{
return true;
}
另外,这个定义也不正确。这会将指针分配到您不想要的位置。
char * StrPos = Str + Pos + Size;
更正为:
char * StrPos = &Str[Pos];
一旦你切断了绳子,我就不知道你想做什么了。我实现了 运行 程序,将剪切的字符串简单地附加到输入的末尾并打印它。你可以在这里看到:
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
bool CutString(char * Str, int Pos, int Size)
{
int StrLen = strlen(Str);
if (Size < 1 || Pos > StrLen)
{
return true;
}
char * StrPos = &Str[Pos];
printf("%s\n", StrPos);
strcat(Str, StrPos);
printf("%s", Str);
return false;
}
int main()
{
char input[26] = "WhatDoesThisDo?";
CutString(input,5,14);
return 0;
}
我正在尝试创建一个函数来剪切字符串的一部分,以便在所需的位置和所需的长度删除该部分。
我收到 SIGSEGV 错误(分段错误),而 运行 "Str[Pos] = 0" 的调试器。我不明白为什么,因为我只是想将这个特定位置的 char 指针设置为 0 或 '\0',这样它就好像它是数组的末尾一样。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char **argv)
{
char * String = malloc(512);
String = "Blobfish is the best creature ever made by nature";
CutString(String, 3, 4);
print("%s", String);
}
bool CutString(char * Str, int Pos, int Size)
{
int StrLen = strlen(Str);
printf("After4\n");
if (Size < 1 || Size + Pos > StrLen) return true;
printf("After4.1\n");
char * StrPos = Str + Pos + Size;
printf("After4.2\n");
printf("%s", StrPos);
Str[Pos] = 0;
printf("After4.3\n");
strcat(Str, StrPos);
printf("After4.4\n");
return false;
}
在此先感谢您的帮助和保重, 朱尔斯
代码正在尝试更改字符串文字,这是未定义的行为。
改为修改分配的内存。
char * String = malloc(512);
// The below only copies the pointer to the string literal, not the string contents.
// String = "Blobfish is the best creature ever made by nature";
strcpy(String, "Blobfish is the best creature ever made by nature");
该函数无效,但足以表明该程序在任何情况下都有未定义的行为,因为它试图修改字符串文字,而修改字符串文字会导致未定义的行为。
char * String = malloc(512);
String = "Blobfish is the best creature ever made by nature";
CutString(String, 3, 4);
//...
此外还有内存泄漏,因为首先分配了内存并将指向内存的指针分配给了指针String。然后指针 String 被重新分配了一个字符串文字的地址。所以分配内存的地址丢失了。
至于函数则可以按如下方式定义,如下面的演示程序所示。
#include <stdio.h>
#include <string.h>
char * CutString( char * s, size_t pos, size_t n )
{
size_t length = strlen( s );
if ( pos < length )
{
n = length - pos < n ? length - pos : n;
memmove( s + pos, s + pos + n, length - pos - n + 1 );
}
return s;
}
int main(void)
{
char s[] = "Blobfish is the best creature ever made by nature";
puts( s );
puts( CutString( s, 3, 4 ) );
return 0;
}
程序输出为
Blobfish is the best creature ever made by nature
Bloh is the best creature ever made by nature
注意函数的return类型bool没有什么意义。当这样的函数 returns 指向修改后的字符串本身时会好得多。所有标准 C 字符串函数都遵循此约定。
我不太清楚你想用这个函数做什么。但我强调了几个问题:
这会给您带来麻烦。因为 Size + Pos 应该总是大于 StrLen。因为 Strlen 应该等于大小。
if (Size < 1 || Size + Pos > StrLen) return true;
改写为:
if (Size < 1 || Pos > StrLen)
{
return true;
}
另外,这个定义也不正确。这会将指针分配到您不想要的位置。
char * StrPos = Str + Pos + Size;
更正为:
char * StrPos = &Str[Pos];
一旦你切断了绳子,我就不知道你想做什么了。我实现了 运行 程序,将剪切的字符串简单地附加到输入的末尾并打印它。你可以在这里看到:
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
bool CutString(char * Str, int Pos, int Size)
{
int StrLen = strlen(Str);
if (Size < 1 || Pos > StrLen)
{
return true;
}
char * StrPos = &Str[Pos];
printf("%s\n", StrPos);
strcat(Str, StrPos);
printf("%s", Str);
return false;
}
int main()
{
char input[26] = "WhatDoesThisDo?";
CutString(input,5,14);
return 0;
}