1

I have a dynamic array of strings initialized in the main function, but I'm not sure how to properly use pointers and send my array to function. In the function_w I want to work with the array and then send it back to main function, and later to use in other functions. This is my code:

char function_w(char **array,FILE **fr)
{
int i = 0, strcount = 0;
int buf_length = 50; 
char buf [buf_length],p = NULL;
fseek(*fr, 0, SEEK_SET);

 while (fgets(buf, buf_length, *fr) != NULL)
 {
    array = (char **)realloc(array, (strcount + 1) * sizeof(char *));
    array[strcount++] = strdup(buf);
 }
return p;
}

int main(void)
{   
    char **array = NULL;
    FILE*fr = NULL; //file is opened in other function, before calling w
...
    if (fr != NULL) **array = function_w(array,&fr);
    else printf("Error");
return 0;
}

Every time I use function_w there's no output. Can you help me?

2
  • My c skills are a little bit outdated but shouldn't it be *fr instead of &fr Commented Nov 9, 2020 at 14:06
  • @SimonDanninger No because you usually won't touch the contents of the structure(?) FILE. It should be fr and the argument should be changed accordingly in my opinion. Commented Nov 9, 2020 at 14:08

1 Answer 1

1
  • To have functions modify what are passed, pass pointers to what should be modified. What should be modified is char** in this case, so a pointer to that is char***.
  • The caller of function_w will want the information strcount, so you should return that.
  • You won't need a pointer to FILE* because you don't modify that.

Applying these, your program should be like this:

int function_w(char ***array,FILE *fr)
{
    int i = 0, strcount = 0;
    int buf_length = 50; 
    char buf [buf_length],p = NULL;
    fseek(fr, 0, SEEK_SET);

    while (fgets(buf, buf_length, fr) != NULL)
    {
        *array = realloc(*array, (strcount + 1) * sizeof(char *));
        (*array)[strcount++] = strdup(buf);
    }
    return strcount;
}

int main(void)
{   
    char **array = NULL;
    FILE*fr = NULL; //file is opened in other function, before calling w
    int strcount;
...
    if (fr != NULL) strcount = function_w(&array,fr);
    else printf("Error");
    return 0;
}
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you! But now I found another problem :( When I want to print my array, it only prints the first item of the array. 'for(i = 0; i < strcount; i++) printf("array[%d] == %s", i, *array[i]);'
@Cassie It is surprising that the code prints the first item of the array. It will cause Segmentation Fault in my view. It is not impossible because anything is allowed on undefined behavior though.
@Cassie Ah, or the code is in the function function_w, not main? Then use (*array)[i] like I did.
I can't believe the problem was with brackets xD. Thank you again. I'm new to programming so I don't understand pointers yet.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.