我正在学习 C 字符串操作并且正在使用strtok()
函数。我的代码以警告告终,然后输出是分段错误。
这是源代码(在文件token3.c中):
#include <stdio.h>
#include <string.h>
int main() {
char str[] = "aa.bb.cc.dd.ee.ff";
char *p;
p = strtok(str, '.');
while (p != NULL) {
printf("%s\n", p);
p = strtok(NULL, '.');
}
return 0;
}
编译过程中的警告:
token3.c: In function ‘main’:
token3.c:6:15: warning: passing argument 2 of ‘strtok’ makes pointer from integer without a cast [-Wint-conversion]
p=strtok(str,'.');
^~~
In file included from token3.c:2:0:
/usr/include/string.h:335:14: note: expected ‘const char * restrict’ but argument is of type ‘int’
extern char *strtok (char *__restrict __s, const char *__restrict __delim)
^~~~~~
token3.c:9:17: warning: passing argument 2 of ‘strtok’ makes pointer from integer without a cast [-Wint-conversion]
p=strtok(NULL,'.');<br>
^~~
In file included from token3.c:2:0:
/usr/include/string.h:335:14: note: expected ‘const char * restrict’
but argument is of type ‘int’
extern char *strtok (char *__restrict __s, const char *__restrict __delim)
^~~~~~<
预期输出:
aa
bb
cc
dd
ee
ff
实际输出:
Segmentation fault(core dumped)