将“,”分隔列表拆分为C中的数组的最佳方法是什么。我知道列表中有多少内容。
char list = "one,two,three,four";
int ENTServerAmount = 8;
char **ENTServer;
ENTServer = malloc(sizeof(char *) * ENTServerAmount);
*** Code that splits "list" into "ENTServer" ***
此外,我不太擅长分配,所以如果我的分配声明错误,请告诉我。
答案 0 :(得分:3)
strtok()
可能是您正在寻找的功能。
char list[] = "one,two,three,four";
int ENTServerAmount = 8;
char **ENTServer;
char *tmp = strtok (str, ",");
int index = 0;
while (pch != NULL)
{
ENTSever[index++] = tmp;
tmp = strtok (NULL, ",");
}