[英]Creating a dynamic array of char* from a char* text using strtok_s
My search here got many results, but none quite like so that I could find a solution to my problem. 我在这里搜索得到了很多结果,但没有一个能使我找到解决问题的方法。
I'm creating a Mathcad UserEFI DLL in C using Visual Studio 2013. I don't want to use strings, only char*. 我正在使用Visual Studio 2013在C中创建Mathcad UserEFI DLL。我不想使用字符串,只能使用char *。
Now, I want to emulate the console main function, which uses char* argv[] to access the parameters of a called executable. 现在,我要模拟控制台主要功能,该功能使用char * argv []访问调用的可执行文件的参数。 Mathcad will call the DLL with a string like "-T=3 zh 13".
Mathcad将使用类似“ -T = 3 zh 13”的字符串来调用DLL。 All I want is to parse this text into an array of char*, just like argv[] would be if I called an executable with this added parameters.
我想要的只是将文本解析为char *数组,就像argv []一样,如果我使用添加的参数调用可执行文件。 I hope I expressed this in an understandable way.
我希望我以一种可以理解的方式表达这一点。 I use strtok_s to parse the text and one has to consider, that every token can have a different size.
我使用strtok_s解析文本,必须考虑到,每个令牌都可以具有不同的大小。
The error must lie in the following function: 该错误必须在于以下功能:
typedef struct tArgReturnType {
int ACount;
char** Argus;
} ARGRETURN;
ARGRETURN ParseStringToArgs(char* text) {
char *token = NULL;
char *nextToken = NULL;
int argCount = 0;
char* temptext = NULL;
strcpy(temptext, text);
char** uebergabe = (char**)malloc(sizeof(char**));
token = strtok_s(temptext, " ", &nextToken);
while (token != NULL) {
argCount++;
uebergabe = (char**)realloc(uebergabe, sizeof(uebergabe)+sizeof(token));
uebergabe[argCount - 1] = token;
token = strtok_s(NULL, " ", &nextToken);
}
ARGRETURN ReturnVar;
ReturnVar.ACount = argCount;
ReturnVar.Argus = (char**)malloc(sizeof(uebergabe));
memcpy(ReturnVar.Argus, uebergabe, sizeof(uebergabe));
free(uebergabe);
return ReturnVar;
}
I'm sure that this is a complete mishmash of heap memory allocation failures (as indicated by the error the mathcad compiler gives me), since I modified this code multiple times while trying to find a solution. 我确信这是堆内存分配失败的完整记录(如mathcad编译器给我的错误所示),因为我在尝试查找解决方案时多次修改了此代码。 I'm just utterly confused now.
我现在完全感到困惑。
typedef struct tArgReturnType { int ACount; char** Argus; } ARGRETURN; ARGRETURN ParseStringToArgs(char* text) { char *token = NULL; char *nextToken = NULL; int argCount = 0; char* temptext = malloc(strlen(text) + 1); strcpy(temptext, text); char** uebergabe = malloc(sizeof(char**)); token = strtok_s(temptext, " ", &nextToken); while (token != NULL) { argCount++; uebergabe = (char**)realloc(uebergabe, sizeof(uebergabe)+sizeof(token)); uebergabe[argCount - 1] = token; token = strtok_s(NULL, " ", &nextToken); } ARGRETURN ReturnVar; ReturnVar.ACount = argCount; ReturnVar.Argus = malloc(sizeof(uebergabe)); memcpy(ReturnVar.Argus, uebergabe, sizeof(uebergabe)); free(uebergabe); free(temptext); return ReturnVar; }
ARGRETURN ParseStringToArgs(const char* text) {
ARGRETURN ReturnVar = { 0 };
int n = 0;
char temp[100];
while (sscanf(text += n, "%99s%n", temp, &n) == 1) {
ReturnVar.Argus = realloc(ReturnVar.Argus, ++ReturnVar.ACount*sizeof(*ReturnVar.Argus));
strcpy(ReturnVar.Argus[ReturnVar.ACount - 1] = malloc(strlen(temp) + 1), temp);
}
return ReturnVar;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.