简体   繁体   English

如何从函数中获取指针的指针

[英]How to get back pointer of pointers from a function

I have a function which open a file, read its content line by line and then push it to an array. 我有一个打开文件的功能,逐行读取其内容,然后将其推入数组。 I have managed to get the array functionnal inside the right function, but when I want to get it back to my main function, I cannot get any items of my array. 我设法在正确的函数中获取了数组函数,但是当我想将其返回到主函数中时,我无法获取数组中的任何项目。

Some code will help you to understand: 一些代码将帮助您理解:

My main function: 我的主要功能:

/* ----------------- MAIN ------------ */
int main(int argc, char *argv[]) {

    /*... some useless code for now ... */

    char **ptrLines = NULL;
    ptrLines = readEventFile(ptrParam, ptrLines);
    outputExecTrace(WAR, "PTRLINES x : %x", ptrLines);
    outputExecTrace(WAR, "PTRLINES char[] : %s", *(ptrLines + 2));

    return EXIT_SUCCESS;
}

My fileReader function: 我的fileReader函数:

char** readEventFile(Parameters *parameters, char **arrLine) {

    FILE *fp = fopen(parameters->inputFilePath, "r");
    if (fp == NULL)
        exit(0);

    char line[128];
    int nbCharOfLine = 0;
    while(1) {
        fgets(line, sizeof(line), fp);
        if (feof(fp))
            break;

        nbCharOfLine++;
    }

    fclose(fp);
    arrLine = malloc(sizeof(line) * nbCharOfLine);
    nbCharOfLine = 0;
    fp = fopen(parameters->inputFilePath, "r");
    while(1) {
        fgets(line, sizeof(line), fp);
        if (line[0] != '#') {
            arrLine[nbCharOfLine] = malloc((strlen(line)+1) * sizeof(char));
            strcpy(arrLine[nbCharOfLine], line);
            nbCharOfLine++;
        }
        if (feof(fp))
            break;
    }

    fclose(fp);
    outputExecTrace(WAR, "ARRLINE x : %x", arrLine);
    outputExecTrace(WAR, "ARRLINE char[] : %s", *(arrLine + 2));

    return arrLine;
}

Has it is, my outputs are the followings: 有了它,我的输出如下:

WARNING: ARRLINE int : -2020552688
WARNING: ARRLINE char[] : 1 3 4 //This is the result I am looking for.

WARNING: PTRLINES int : -2020552688 // Same as ARRLINE
Segmentation fault (core dumped) // And this is because ptrLines[2] doesn't contains anything... but why ?!

How can I fix this ? 我怎样才能解决这个问题 ?

It's hard to tell exactly what happen, because you have several small problems within code, which could lead to this, try to change your code in following way: 很难确切说明会发生什么,因为代码中存在几个小问题(可能会导致此问题),请尝试通过以下方式更改代码:

char** readEventFile(char* fileName) {
    char line[128];
    int nbCharOfLine = 0;

    FILE *fp = fopen(fileName, "r");
    if (fp == NULL)
        exit(1);
    while (!feof(fp)) {
        char *r = fgets(line, sizeof(line), fp); // NOTE: proper handling of fgets return value
        if ((r != NULL) && (line[0] != '#'))
            nbCharOfLine++;
    }
    fclose(fp);

    char **arrLine = calloc(nbCharOfLine, sizeof(char*)); // use calloc to be sure
    nbCharOfLine = 0;

    fp = fopen(fileName, "r");
    while (!feof(fp)) {
        char *r = fgets(line, sizeof(line), fp);
        if ((r != NULL) && (line[0] != '#')) {
            arrLine[nbCharOfLine] = calloc(strlen(line) + 1, sizeof(char));
            strcpy(arrLine[nbCharOfLine++], line);
        }
    }
    fclose(fp);

    return arrLine;
}

in your main, you will call this function like: 在您的主目录中,您将调用此函数,例如:

char **ptrLines = readEventFile(ptrParam->inputFilePath);

Not so sure this is the whole problem, but the posted code: 不太确定这是整个问题,但已发布代码:

while(1) {
    fgets(line, sizeof(line), fp);
    if (line[0] != '#') {
        arrLine[nbCharOfLine] = malloc((strlen(line)+1) * sizeof(char));
        strcpy(arrLine[nbCharOfLine], line);
        nbCharOfLine++;
    }
    if (feof(fp))
        break;
}

is not the right approach for several reasons, including that function feof() is only asserted 'true' when the code has tried to read past the end of the file. 出于多种原因,这是不正确的方法,包括仅当代码尝试读取文件末尾时才将函数feof()声明为“ true”。

suggest: 建议:

while( fgets(line, sizeof(line), fp) )
{
    if (line[0] != '#') 
    {
        if( NULL == (arrLine[nbCharOfLine] = malloc((strlen(line)+1) * sizeof(char)) ) )
        { // then, malloc failed
            perror( "malloc failed" );
            // close file and free all malloc'd areas
            exit( EXIT_FAILURE );
        }

        // implied else, malloc successful

        strcpy(arrLine[nbCharOfLine], line);
        nbCharOfLine++;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM