p******s 发帖数: 938 | 1 【 以下文字转载自 Programming 讨论区 】
【 原文由 phageous 所发表 】
要利用strtok来进行一些string的操作,结果却是segmentation fault,
code如下:
#include
#include
void main() {
char *str;
char *line="15:wildwood.eecs.umich.edu:018032:24.79 wildwood.eecs.umich.edu:018031:21.11 wildwood.eecs.umich.edu:018044:14.83 wildwood.eecs.umich.edu:018093:4.32";
str=strtok(line, ":");
printf("First token is %s\n", str);
while( (str=strtok(NULL, ":")) !=NULL)
printf("Next token is %s \n", str | c******r 发帖数: 512 | 2 Never use this function. It modifies the first arguments.
In your case, it is the const string. Disaster will happen,
you're lucky that it happens soon enough.
【在 p******s 的大作中提到】 : 【 以下文字转载自 Programming 讨论区 】 : 【 原文由 phageous 所发表 】 : 要利用strtok来进行一些string的操作,结果却是segmentation fault, : code如下: : #include : #include : void main() { : char *str; : char *line="15:wildwood.eecs.umich.edu:018032:24.79 wildwood.eecs.umich.edu:018031:21.11 wildwood.eecs.umich.edu:018044:14.83 wildwood.eecs.umich.edu:018093:4.32"; : str=strtok(line, ":");
| c****d 发帖数: 116 | 3 Please change the line:
char *line=.....
to
char line[1024]=....
to see what's the problem.
【 以下文字转载自 Programming 讨论区 】
【 原文由 phageous 所发表 】
要利用strtok来进行一些string的操作,结果却是segmentation fault,
code如下:
#include
#include
void main() {
char *str;
char *line="15:wildwood.eecs.umich.edu:018032:24.79 wildwood.eecs.umich.edu:018031:21.11 wildwood.eecs.umich.edu:018044:14.83 wildwood.eecs.umich.edu:018093:4.32";
str=strtok(line, ":");
printf("First token is %
【在 p******s 的大作中提到】 : 【 以下文字转载自 Programming 讨论区 】 : 【 原文由 phageous 所发表 】 : 要利用strtok来进行一些string的操作,结果却是segmentation fault, : code如下: : #include : #include : void main() { : char *str; : char *line="15:wildwood.eecs.umich.edu:018032:24.79 wildwood.eecs.umich.edu:018031:21.11 wildwood.eecs.umich.edu:018044:14.83 wildwood.eecs.umich.edu:018093:4.32"; : str=strtok(line, ":");
| c**t 发帖数: 2744 | 4 好奇怪哦,编译 Hello World 时,得到如下 warning:
line 10: warning: improper pointer/integer combination: arg #2
运行后得到Segmentation fault.
删掉 core 后重新编译,居然一切正常,连warning也没有.
【在 p******s 的大作中提到】 : 【 以下文字转载自 Programming 讨论区 】 : 【 原文由 phageous 所发表 】 : 要利用strtok来进行一些string的操作,结果却是segmentation fault, : code如下: : #include : #include : void main() { : char *str; : char *line="15:wildwood.eecs.umich.edu:018032:24.79 wildwood.eecs.umich.edu:018031:21.11 wildwood.eecs.umich.edu:018044:14.83 wildwood.eecs.umich.edu:018093:4.32"; : str=strtok(line, ":");
|
|