函数strtok造成段错误,但不是在通过code步骤 [英] strtok causing segfault but not when step through code
问题描述
我是新的C,我试图分裂日期/时间字符串转换成不同的变量。然而,当我通过GDB线code。通过行一步,它的工作原理,但是,当我让它通过正常没有它赛格故障断点运行,我不明白为什么。
I am new to C and I am trying to split a date/time string into separate variables. However, when I step through the code in gdb line by line, it works, however, when I let it run through normally without breakpoints it seg faults and I can't see why.
下面是code:
char * dateTimeString = "2011/04/16 00:00";
char dateVar[11];
char timeVar[6];
if (splitTimeAndDateString(dateVar, timeVar, dateTimeString))
{
exit(1);
}
printf("Date: %s\tTime: %s\n", dateVar, timeVar);
下面是函数
int splitTimeAndDateString(char date[11], char time[6], char * dateString)
{
char *token;
token = strtok(dateString, " ");
int i = 0;
while (token != NULL)
{
if (i == 0)
{
strcpy(date, token);
}
else if (i == 1)
{
strcpy(time, token);
}
else
{
printf("Overrun date time string\n");
return 1;
}
token = strtok(NULL, " ");
i++;
}
return 0;
}
感谢您的帮助,您可以提供。
Thanks for any help you can provide.
推荐答案
的的strtok()
函数修改,你要分析,并更换所有的分隔符字符串 \\ 0
NUL符号。
The strtok()
function modifies string that you wants to parse, and replace all delimiters with \0
nul symbol.
阅读:的char * strtok的(字符*海峡,为const char *分隔符);
海峡结果
C字符串截断。结果
注意,这个字符串的内容
被修改并分解成较小的字符串(令牌)。 Alternativelly,
空指针可以指定,在这种情况下,功能继续
扫描其中previous成功调用该函数结束了。
str
C string to truncate.
Notice that the contents of this string are modified and broken into smaller strings (tokens). Alternativelly, a null pointer may be specified, in which case the function continues scanning where a previous successful call to the function ended.
在您的code:
strtok(dateString, " ");
^
| is a constant string literal
dateString
点2011年4月16日00:00
一个常量字符串,并且通过使用的strtok()
您code试图写只读存储器 - 这是非法的,这引起了分段错误。
dateString
points to "2011/04/16 00:00"
a constant string literal, and by using strtok()
your code trying to write on read-only memory - that is illegal and this caused segmentation fault.
阅读<一href=\"http://stackoverflow.com/questions/17104953/c-strtok-split-string-into-tokens-but-keep-old-data-unaltered/17104999#17104999\">linked答案图了解:如何的strtok()
作品?
Read this linked answer for diagram to understand: how strtok()
works?
编辑:
@: 的char * strtok的(字符*海峡,为const char *分隔符);
在给定的code例如, STR
是一个数组,而不是常量字符串。它的声明:
@: char * strtok ( char * str, const char * delimiters );
In given code example, str
is an array, not constant string literal. Its declaration:
char str[] ="- This, a sample string.";
下面海峡[]
被终止的字符数组的NUL,与字符串初始化其长度等于指定字符串的大小。您可以更改的海峡[]
例如内容海峡[I] ='A'
是一个有效的操作。
Here str[]
is an nul terminated array of chars, that initialized with string and its length is equals to size of the assigned string. You can change the content of str[]
e.g. str[i] = 'A'
is a valid operation.
而在你的code:
char * dateTimeString = "2011/04/16 00:00";
dateTimeString
是指向字符串是不可修改的如 dateTimeString [I] ='A'
是一个非法操作这个时候。
dateTimeString
is pointer to string literal that is not modifiable e.g dateTimeString[i] = 'A'
is an illegal operation this time.
这篇关于函数strtok造成段错误,但不是在通过code步骤的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!