【问题标题】:How can I store an array in a MD array, and return that array from function?如何将数组存储在 MD 数组中,并从函数中返回该数组?
【发布时间】:2018-05-16 22:12:51
【问题描述】:

我有一个方法可以接收文件列表,并获取有关文件的数据。这些文件的格式作为两个一维数组返回。我想将它们组合成一个二维数组。

文件只返回一个类型数组和一个数据数组。

这是有问题的方法。我已成功从文件中获取所需的数据并放入一维数组,但是当我尝试将它们添加到二维数组时,它会运行(但会发出警告,即 char * 和 char *[][] are' t 等效)。

char *ParseFiles(char *listOfFiles[32])
{
    char *allFileData;
    char *tmpArray[32][32];

    allFileData = tmpArray;

    for(int i = 0; i < listOfFiles[i]; i++)
    {
        int j = 0;
        // Get the File Type
        char *currentFileType = GetFileType(listOfFiles[i], strlen(listOfFiles[i]));

        if(currentFileType)
        {
            // Warning char * and char *[][] are not equivalent.
            tmpArray[i][j] = currentFileType;
            printf("File %d is %s\n", i, currentFileType);
            //printf("File %d is %s\n", i, &tmpArray[i][j]);
            //free(currentFileType);
        }

        //Get all File Data
        // Warning char * and char *[][] are not equivalent.
        char *currentFileData = GetFileData(listOfFiles[i]);

        j = j + 1;
        if(currentFileData)
        {
            tmpArray[i][j] = currentFileData;
            printf("File Data:\n%s\n", currentFileData);
            //free(currentFileType);
        }
    }
    return allFileData;
}

当我尝试在被注释掉的 printf 语句中输出 tmpArray[][] 值时,我得到了意外的字符(对于犯错的人来说是意外的)?]p

问题是基于两个char数组不是同一类型的警告,还是因为我试图将单维数组错误地插入多维数组?

我得到的确切警告是warning: incompatible pointer types assigning to 'char*' from 'char *[32][32]'


下面是我的另外两种方法,也是我的主要方法:

char *GetFileType(char *filePath, int size)
{
    char *ret = malloc(size);

    char currentLine[100];
    FILE *file = fopen(filePath, "r");

    while(fgets(currentLine, sizeof(currentLine), file) != NULL)
    {
        if(strstr(currentLine, "test"))
        {
            ret = "test";
        }

        else if(strstr(currentLine, "production"))
        {
            ret = "production";
        }
    }
    if(!ret)
    {
        return NULL;
    }
    return ret;

}

char *GetFileData(char *filePath)
{

    char *buffer = NULL;
    size_t size = 0;

    FILE *file = fopen(filePath, "r");

    // Get the buffer size
    fseek(file, 0, SEEK_END);
    size = ftell(file);

    // Reset the position of the stream
    rewind(file);

    buffer = malloc((size + 1) * sizeof(*buffer));

    // Read the entire file into the buffer
    fread(buffer, size, 1, file);

    buffer[size] = '\0';

    return buffer;
}

int main (void)
{
    char *listOfFiles[32] = {"file1.txt", "file2.txt", "file3.txt"};

    ParseFiles(listOfFiles);

    printf("Press ENTER key to Continue");
    getchar();
    return 0;
}

【问题讨论】:

  • 请注意sizeof(listOfFiles[i]) 会给你指针的大小而不是它指向的字符串的长度,你需要strlen。
  • 另请注意,您的GetFileType 函数中存在内存泄漏。如果你想复制一个字符串,你需要使用strcpy。另外不要忘记 C 中的 char 字符串实际上称为 null-terminated 字节字符串。 null-terminator 也需要为其分配空间。
  • 感谢您告诉我。我修复了sizeof,我正在调查你在GetFileType 上的意思。我将通过添加空终止符来处理空终止字节字符串,就像在 GetFileData 中处理它一样。不过,我不确定我明白你所说的需要 strcpy 是什么意思。
  • 首先你做char *ret = malloc(size);。这使得ret 指向您分配的内存。然后你做例如ret = "test";。这使得ret 指向其他地方,您丢失了指向您分配的内存的原始指针。而且由于ret指向的内存不是malloc分配的,你不能再将指针传递给free。

标签: c arrays pointers character


【解决方案1】:

问题不在你说的地方,而是在这里:

char *allFileData;
char *tmpArray[32][32];

allFileData = tmpArray;

tmpArray 的类型是char *[32][32]。 allFileData 的类型是 char *。该分配根本无效。

还有一件更糟糕的事情发生了:你返回的 allFileData 指向 局部变量 tmpArray。一旦函数返回,变量tmpArray就会超出作用域,你返回的指针会立即失效。要么将数组作为参数传入,要么在函数内部动态分配。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-06-14
    • 2021-11-28
    • 2020-04-08
    • 2016-11-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多