我正在尝试使用C创建函数,该函数应使用定界符分割字符串并将每行打印到新行。如果我以字符串形式给出char str_working [] =“ First,Second,Third”;在功能内部,一切似乎都能正常工作。如果我将string作为函数的参数,则该函数不会显示任何内容。
#include <stdio.h>
#include <inttypes.h>
#include <string.h>
void seperate(char *str, char *sep);
int main() {
seperate("First,Second,Third", ",");
return 0;
}
void seperate(char *str, char *sep) {
// This is showing "First,Second,Third"
printf("%s\n", str);
// When I use this one instead of str everything works fine!
// char str_working[] = "First,Second,Third";
char *token = strtok(str, sep);
// Nothing gets printed here!
while (token != NULL)
{
printf("%s\n", token);
token = strtok(NULL, sep);
}
}