C语言字符串处理函数strtok

C语言字符串处理函数strtok第二次及以 后的调用中第一个参数要用NULL的原因是什么?能不能解释下原理?

#include <string.h> char *strtok( char *str1, const char *str2 ); 功能:函数返回字符串str1中紧接“标记”的部分的指针, 字符串str2是作为标记的分隔符。如果分隔标记没有找到,函数返回NULL。为了将字符串转换成标记,第一次调用str1 指向作为标记的分隔符。之后所以的调用str1 都应为NULL。
例如:
char str[] = "now # is the time for all # good men to come to the # aid of their country"; char delims[] = "#"; char *result = NULL; result = strtok( str, delims ); while( result != NULL ) { printf( "result is \"%s\"\n", result ); result = strtok( NULL, delims ); } 以上代码的运行结果是:
result is "now " result is " is the time for all " result is " good men to come to the " result is " aid of their country" 相关主题:
温馨提示:答案为网友推荐,仅供参考
相似回答