0

I want to refactor a function so that I can use it for arrays of different lengths and return that newly created array so that other functions can access it. I can't make the array static since you can't have static arrays with dynamic length. I also can't use a global struct because that needs to take the length of the array and that has to be hardcoded I think. So the question is whether it is even possible to do something like this:

char* splitElementsArr(FILE* file){
    int length = countBlankLines(file);
    char *arr[length] // or maybe use malloc here

    ...Some operations to fill array

    return arr;
Phil
  • 321
  • 1
  • 17

1 Answers1

0

No, you can't. You are returning a pointer to local data that were dynamically allocated on the stack. As soon as you return, those data no longer exist.

Solution : use malloc().

xhienne
  • 5,738
  • 1
  • 15
  • 34