gpt4 book ai didi

c - 在 C 中将 ","分隔列表拆分为数组的最佳方法是什么?

转载 作者:行者123 更新时间:2023-11-30 21:34:49 27 4
gpt4 key购买 nike

Possible Duplicate:
Split string with delimiters in C

在 C 中将“,”分隔的列表拆分为数组的最佳方法是什么。我知道列表中有多少内容。

char list = "one,two,three,four";
int ENTServerAmount = 8;
char **ENTServer;
ENTServer = malloc(sizeof(char *) * ENTServerAmount);
*** Code that splits "list" into "ENTServer" ***

此外,我不太擅长分配,所以如果我的分配语句错误,请告诉我。

最佳答案

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, ",");
}

关于c - 在 C 中将 ","分隔列表拆分为数组的最佳方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11510368/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com