Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/react-native/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 访问strtok的字符数组指针中传递的变量时获取不正确的值_C_Arrays_Pointers_Strtok - Fatal编程技术网

C 访问strtok的字符数组指针中传递的变量时获取不正确的值

C 访问strtok的字符数组指针中传递的变量时获取不正确的值,c,arrays,pointers,strtok,C,Arrays,Pointers,Strtok,这是我的密码 //Split up the config by lines int x; int numberOfConfigLines = 0; for (x = 0; x < strlen(buffer); x++) { if (buffer[x] == '\n') { numberOfConfigLines++; } } char *configLines[numberOfConfigLines]; tokenize(configLines, buff

这是我的密码

//Split up the config by lines
int x;
int numberOfConfigLines = 0;
for (x = 0; x < strlen(buffer); x++)
{
    if (buffer[x] == '\n') {
        numberOfConfigLines++;
    }
}
char *configLines[numberOfConfigLines];
tokenize(configLines, buffer, "\n", numberOfConfigLines);
//将配置按行拆分
int x;
int numberOfConfigLines=0;
对于(x=0;x
此函数的思想是计算缓冲区中的换行数,然后使用以下方法将缓冲区拆分为strtok数组:

#include <string.h>
#include <stdlib.h>

void tokenize(char **arrToStoreTokens, char *delimitedString, char *delimiter, int expectedTokenArraySize) {
    //Create a clone of the original string to prevent making permanent changes to it
    char *tempString = (char *)malloc(strlen(delimitedString) + 1);
    strcpy(tempString, delimitedString);

    if (expectedTokenArraySize >= 1) {
    arrToStoreTokens[0] = strtok(tempString, delimiter);

        int x;
        for (x = 1; x < expectedTokenArraySize; x++ ) {
            arrToStoreTokens[x] = strtok(NULL, delimiter);
        }
    }

    //Dispose of temporary clone
    free(tempString); 
}
#包括
#包括
void tokenize(char**arrToStoreTokens,char*delimitedString,char*delimiter,int expectedTokenArraySize){
//创建原始字符串的克隆以防止对其进行永久性更改
char*tempString=(char*)malloc(strlen(delimitedString)+1);
strcpy(tempString,delimitedString);
如果(预期Tokenarray大小>=1){
arrtostoretokes[0]=strtok(tempString,分隔符);
int x;
对于(x=1;x
如果我直接访问
arrToStoreTokens[0]
,我会得到正确的结果,但是当我尝试访问
configLines[0]
时,
tokenize
函数结束后,我会得到不同的结果(可能是未知字符或只是空的)

此外,我相信这只是在我开始以root用户身份运行程序时才开始发生的(对于不同的需求)-尽管我可能错了。-编辑:确认不是问题所在

有什么想法吗?

不会重新分配任何东西。它只是你给它的东西的切入点

数组存储strtok提供给您的指针,但不复制内容

因此,如果释放
tempString
变量,则释放由strtok的返回值指向的数据。你必须保留它,并在最后释放它

或者,您可以对每次返回的strtok进行复制,将其存储在数组中,以制作每个令牌的真实副本,但在这种情况下,您必须在最后释放每个令牌

第二种解决方案如下所示:

void tokenize(char **arrToStoreTokens, char *delimitedString, char *delimiter, int expectedTokenArraySize) {
    //Create a clone of the original string to prevent making permanent changes to it
    char *tempString = (char *)malloc(strlen(delimitedString) + 1);
    strcpy(tempString, delimitedString);

    if (expectedTokenArraySize >= 1) {
    arrToStoreTokens[0] = strdup(strtok(tempString, delimiter)); // Here is the new part : strdup

        int x;
        for (x = 1; x < expectedTokenArraySize; x++ ) {
            arrToStoreTokens[x] = strdup(strtok(NULL, delimiter)); // Here is the new part : strdup
        }
    }

    //Dispose of temporary clone
    free(tempString); 
}
void deleteTokens(char **arrToStoreTokens, int arraySize)
{
    int x;
    for (x = 0; x < arraySize; ++x)
    {
        free(arrToStoreTokens[x]);
    }
}
void标记化(char**arrToStoreTokens,char*delimitedString,char*delimiter,int-expectedTokenArraySize){
//创建原始字符串的克隆以防止对其进行永久性更改
char*tempString=(char*)malloc(strlen(delimitedString)+1);
strcpy(tempString,delimitedString);
如果(预期Tokenarray大小>=1){
arrToStoreTokens[0]=strdup(strtok(tempString,delimiter));//这是新的部分:strdup
int x;
对于(x=1;x
使用此数组后,您必须使用如下功能删除它:

void tokenize(char **arrToStoreTokens, char *delimitedString, char *delimiter, int expectedTokenArraySize) {
    //Create a clone of the original string to prevent making permanent changes to it
    char *tempString = (char *)malloc(strlen(delimitedString) + 1);
    strcpy(tempString, delimitedString);

    if (expectedTokenArraySize >= 1) {
    arrToStoreTokens[0] = strdup(strtok(tempString, delimiter)); // Here is the new part : strdup

        int x;
        for (x = 1; x < expectedTokenArraySize; x++ ) {
            arrToStoreTokens[x] = strdup(strtok(NULL, delimiter)); // Here is the new part : strdup
        }
    }

    //Dispose of temporary clone
    free(tempString); 
}
void deleteTokens(char **arrToStoreTokens, int arraySize)
{
    int x;
    for (x = 0; x < arraySize; ++x)
    {
        free(arrToStoreTokens[x]);
    }
}
void deleteTokes(char**arrtostoreTokes,int-arraySize)
{
int x;
对于(x=0;x
是的,刚刚检查过,当不在rootBTW中时问题仍然存在,不要播放
malloc
和family的结果。你能用正确的版本回答吗,我不是来自内存分配背景,但我想我是在将strtok的输出写入'arrToStoreTokens[x]`?你没有复制内容,只有地址。我编辑了我的答案。你能用修改过的代码编辑你的答案吗?添加了示例。还有麻烦吗?