0

I'm working on an assignment in which I'm required to have a method return a pointer to an array of strings, read from a file. The function works, but when I try to assign the returned pointer to a variable in the main function, I end up with an array of null values. Here's my code:

#include <stdio.h>

char **buildAnsArray(){

    FILE *in;
    int num, i;

    in = fopen("data.txt", "r");
    fscanf(in, "%d", &num);

    char ansArray[10][num];

    for(i = 0; i < num; i++){
        fscanf(in, "%s", ansArray[i]);
    }

    return ansArray;
};


main(){
    char **ansArray = buildAnsArray();
}

The input file (data.txt) that I'm working with begins with:

3
B
20
1101

ansArray (in buildAnsArray) correctly contains {'B', '20', '1101'}, but ansArray (in main) is empty. I know I could just move everything to main and not worry about returning the pointer to the string array, but the assignment requires the buildAnsArray function. What can I do to fix this?

4
  • possible duplicate of String is not captured from return value in function in C Commented Oct 22, 2014 at 20:53
  • 3
    Not 100% sure this is related to your problem, but if you want to return a pointer from a function you should allocate your array on the heap (using malloc). Right now you are creating your array on the stack, which means it can (and will) get cleaned up eventually after your function call Commented Oct 22, 2014 at 20:53
  • 3
    This has many duplicates in different forms, but ultimately it boils down to this: You're returning an address of an automatic variable (i.e. it no longer exists once your function returns, therefore invoking undefined behavior). Adding further logs to the fire, the resulting pointer-type isn't even compatible with your declared result type of the function (char** is not the same as char[][N]). If your compiler didn't warn you about either, you need to turn up your warning levels to be considerably more verbose. Commented Oct 22, 2014 at 20:57
  • Note that the semicolon after the final close brace of the function is actually an empty declaration. Don't write semicolons where they aren't needed. Commented Oct 22, 2014 at 21:07

1 Answer 1

1
char ansArray[10][num];
return ansArray;

is a temporary variable. It is destroyed as soon as the function returns.

Do this: ...

char** ansArray = malloc(num * sizeof(char*));
for(i = 0; i < num; i++){
    char* item = malloc(10*sizeof(char));

    fscanf(in, "%s", item);
    ansArray[i] = item;

}

... return ansArray;

and then

main(){

    char **ansArray = buildAnsArray();
    printf("%s", ansArray[1]);
    free(ansArray);
}

Also, since the number of strings is allocated in the function, you need to return num also so the individual items in ansarray can be freed, which isn't shown above.

Sign up to request clarification or add additional context in comments.

3 Comments

the question is tagged C, suggesting C++ code (new and delete) is not helpful
What exactly have you written? new? delete? This isn't C++ the author has posted, it's C
That did the trick! I had tried malloc before, but I hadn't thought to malloc individual items and add them to an array. Thank you very much!

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.