吐出字符串时出现C分段错误
C Segmentation fault when spitting a string
首先,如果这是一个基本(或愚蠢)的问题,我很抱歉,我来自 Python 并且我是 C 语言的新手(仍在研究它)。
我有一个简短的脚本可以将字符串拆分为子字符串,
例如:"this is my -string" 变成 "this"、"is"、"my"、"-string"。
之后我想select以字符开头的子字符串:'-',并保存在一个变量调用"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;
}
while 循环内一切正常,我什至可以打印变量 "subline",但在 while 循环外我遇到分段错误,输出如下:
root@debian:/home/user/Desktop/splits# ./split
Split string in tokens:
this
is
my
subline: string
string
Segmentation fault
我试图找出并使用 malloc(sizeof(*subline)) 解决它;但在 while 循环之外总是出现相同的分段错误。
有人知道吗?
谢谢。
当p
变为null时,你还是传给了strcncmp()
。不要那样做 - 添加另一张支票。
当代码在 while 循环中检测到没有更多匹配项时,strtok
返回 NULL,但在循环逻辑捕获它之前,调用 strncmp()
方法NULL 指针。
这是主要错误:
在处理第一个字符串指针之前第二次调用函数 strtok()
这一行:
memcpy(subline, ++p, strlen(p)+1);
(记住 'p' 是指向字符串第一个字符的指针,由于某些 'side effects' .
#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] = {'[=11=]'}; // 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 '[=11=]'
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;
}
首先,如果这是一个基本(或愚蠢)的问题,我很抱歉,我来自 Python 并且我是 C 语言的新手(仍在研究它)。
我有一个简短的脚本可以将字符串拆分为子字符串, 例如:"this is my -string" 变成 "this"、"is"、"my"、"-string"。
之后我想select以字符开头的子字符串:'-',并保存在一个变量调用"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;
}
while 循环内一切正常,我什至可以打印变量 "subline",但在 while 循环外我遇到分段错误,输出如下:
root@debian:/home/user/Desktop/splits# ./split
Split string in tokens:
this
is
my
subline: string
string
Segmentation fault
我试图找出并使用 malloc(sizeof(*subline)) 解决它;但在 while 循环之外总是出现相同的分段错误。
有人知道吗?
谢谢。
当p
变为null时,你还是传给了strcncmp()
。不要那样做 - 添加另一张支票。
strtok
返回 NULL,但在循环逻辑捕获它之前,调用 strncmp()
方法NULL 指针。
这是主要错误:
在处理第一个字符串指针之前第二次调用函数 strtok()
这一行:
memcpy(subline, ++p, strlen(p)+1);
(记住 'p' 是指向字符串第一个字符的指针,由于某些 'side effects' .
#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] = {'[=11=]'}; // 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 '[=11=]'
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;
}