[英]C Segmentation fault when spitting a string
First of all sorry if I this is a basic (or stupid) question, I am coming from Python and I am quite new in C (still studying it). 首先,很抱歉,如果我这是一个基本的(或愚蠢的)问题,我来自Python,而我在C语言方面还是一个新手(仍然在研究它)。
I have a short script to split a string into sub-strings, for example: "this is my -string" into "this","is","my","-string". 我有一个简短的脚本,用于将字符串拆分为子字符串,例如:“ this is my -string”分解为“ this”,“ is”,“ my”,“-string”。
After that I want to select the substring that starts with the char: '-', and save in a variable calls "subline": 之后,我要选择以char:'-'开头的子字符串,并保存在一个名为“ subline”的变量中:
#include <stdio.h>
#include <string.h>
#define MAX_CHAR 9999
int main ()
{
char line[] ="this is my -string";
char *p;
char subline[MAX_CHAR];
printf ("Split string in tokens:\n");
p = strtok (line," ");
while (p != NULL)
{
printf ("%s\n", p);
p = strtok (NULL, " ,");
if ((strncmp(p, "-", 1) == 0)){
memcpy(subline, ++p, strlen(p)+1);
printf ("subline: %s\n", subline);
}
}
printf ("\nData:\n");
printf ("subline is: %s\n", subline);
return 0;
}
Everything runs fine inside the while loop, where I can even print the variable "subline", but outside the while loop I get a segmentation fault, here is the output: 在while循环内,一切运行良好,我什至可以打印变量“ subline”,但是在while循环外,我遇到了分段错误,这是输出:
root@debian:/home/user/Desktop/splits# ./split
Split string in tokens:
this
is
my
subline: string
string
Segmentation fault
I tried to figure out and solved it using malloc(sizeof(*subline)); 我试图找出并使用malloc(sizeof(* subline))解决了它; but always the same segmentation fault outside the while loop.
但在while循环外始终存在相同的分段错误。
Anyone has any idea? 有人知道吗?
Thank you. 谢谢。
When p
becomes null, you still pass it to strcncmp()
. 当
p
变为null时,您仍将其传递给strcncmp()
。 Don't do that -- add another check instead. 不要那样做-而是添加另一个检查。
NULL is returned by strtok
when there are no more matches, which the code detects in the while-loop, but before the loop logic will catch it, the strncmp()
method is called with the NULL pointer. 当代码在while循环中检测到没有更多匹配项时,
strtok
将返回NULL,但是在循环逻辑捕获到该strncmp()
之前,将使用NULL指针调用strncmp()
方法。
this is the main error: 这是主要错误:
the function strtok() is being called a second time, before the first string pointer has been processed 在处理第一个字符串指针之前,第二次调用函数strtok()
this line: 这行:
memcpy(subline, ++p, strlen(p)+1);
(remembering that 'p' is a pointer to the first character of the string, and pre-increment of that pointer inside the memcpy() function is a very bad idea due to certain 'side effects'. (请记住,“ p”是指向字符串第一个字符的指针,由于某些“副作用”,在memcpy()函数内部对该指针进行预递增是一个非常糟糕的主意。
#include <stdio.h>
#include <string.h>
// wrap numerics in parens to avoid certain 'text replacement' errors
#define MAX_CHAR (9999)
int main ( void )
{
char line[] ="this is my -string";
char *p;
char subline[MAX_CHAR] = {'\0'}; // assure the string will be properly terminated
printf ("Split string in tokens:\n");
p = strtok (line," "); // p now points to first token (or contains NULL
while (p) // will continue to loop while p not equal to NULL, where NULL is the same as 'false'
{
printf ("%s\n", p);
// don't need all this power function call
// if ((strncmp(p, "-", 1) == 0))
if( '-' == p[0] )
{
p++; // step past the '-'
memcpy(subline, p, strlen(p)); // remember, subline is init. to all '\0'
printf ("subline: %s\n", subline);
}
p = strtok (NULL, " ,"); // p now points to next token (or contains NULL)
} // end while
printf ("\nData:\n");
printf ("subline is: %s\n", subline);
return 0;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.