在 C 中通过 strtok 实现拆分字符串 - 分段错误(核心转储)
implement split string by strtok in C - Segmentation fault (core dumped)
这里有一个在C中用strtok
实现拆分字符串的方法
void split(char** dest, char* src, const char* del){
char* token = strtok(src, del);
while(token!=NULL){
*dest++ = token;
token = strtok(NULL, del);
}
}
我试图通过 main()
测试此功能。测试设备为asus T100(32bit OS, x64 processor) Cygwin编译,gcc version 4.9.2 (GCC)
int main(void){
char* str="/storage/SD:/storage/USB1";
const char* del=":";
char* arr[2];
split(arr, str, del);
return 0;
}
结果是Segmentation fault (core dumped),为什么?
sizeof(char*)
在此测试设备上是4字节。
如果我修改
char* str="/storage/SD:/storage/USB1";
到
char str[]="/storage/SD:/storage/USB1";
一切如预期运行。
C 和 C++ 中的字符串文字是不可变的。任何修改字符串文字的尝试都会导致未定义的行为。函数 strtok 更改作为参数传递给它的字符串。替换此声明
char* str = "/storage/SD:/storage/USB1";
和
char str[] = "/storage/SD:/storage/USB1";
来自 C 标准(6.4.5 字符串文字)
7 It is unspecified whether these arrays are distinct provided their
elements have the appropriate values. If the program attempts to
modify such an array, the behavior is undefined.
从函数的描述来看strtok
4 The strtok function then searches from there for a character that is
contained in the current separator string. If no such character is
found, the current token extends to the end of the string pointed to
by s1, and subsequent searches for a token will return a null pointer.
If such a character is found, it is overwritten by a null character, which terminates the current token. The strtok function saves a
pointer to the following character, from which the next search for a
token will start.
还要考虑到函数 main 应具有 return 类型 int
。
int main( void )
这里有一个在C中用strtok
实现拆分字符串的方法
void split(char** dest, char* src, const char* del){
char* token = strtok(src, del);
while(token!=NULL){
*dest++ = token;
token = strtok(NULL, del);
}
}
我试图通过 main()
测试此功能。测试设备为asus T100(32bit OS, x64 processor) Cygwin编译,gcc version 4.9.2 (GCC)
int main(void){
char* str="/storage/SD:/storage/USB1";
const char* del=":";
char* arr[2];
split(arr, str, del);
return 0;
}
结果是Segmentation fault (core dumped),为什么?
sizeof(char*)
在此测试设备上是4字节。
如果我修改
char* str="/storage/SD:/storage/USB1";
到
char str[]="/storage/SD:/storage/USB1";
一切如预期运行。
C 和 C++ 中的字符串文字是不可变的。任何修改字符串文字的尝试都会导致未定义的行为。函数 strtok 更改作为参数传递给它的字符串。替换此声明
char* str = "/storage/SD:/storage/USB1";
和
char str[] = "/storage/SD:/storage/USB1";
来自 C 标准(6.4.5 字符串文字)
7 It is unspecified whether these arrays are distinct provided their elements have the appropriate values. If the program attempts to modify such an array, the behavior is undefined.
从函数的描述来看strtok
4 The strtok function then searches from there for a character that is contained in the current separator string. If no such character is found, the current token extends to the end of the string pointed to by s1, and subsequent searches for a token will return a null pointer. If such a character is found, it is overwritten by a null character, which terminates the current token. The strtok function saves a pointer to the following character, from which the next search for a token will start.
还要考虑到函数 main 应具有 return 类型 int
。
int main( void )