【发布时间】:2011-09-10 01:11:51
【问题描述】:
我正在使用一个简单的程序来使用 strtok 函数对字符串进行标记。这是代码-
# include <stdio.h>
char str[] = "now # time for all # good men to # aid of their country"; //line a
char delims[] = "#";
char *result = NULL;
result = strtok( str, delims );
while( result != NULL ) {
printf( "result is \"%s\"\n", result );
result = strtok( NULL, delims );
}
程序运行成功。但是,如果将 a 行更改为
char * str= "now # time for all # good men to # aid of their country"; //line a
strtok 函数提供核心转储。我想解释一下我的理解为什么会这样?因为从 strtok 的声明为 --char *strtok( char *str1, const char *str2 ); char *str 作为第一个参数应该可以工作
【问题讨论】:
-
数组不是指针,指针也不是数组。你可能会喜欢the c-faq,尤其是第 6 节。