Looks like you want contents of word
array to be preserved between loop iterations. This means, you must put the array to the scope outside the loop. In your question code, you want to determine size inside the loop, so you'd essentially need to redefine the size of VLA, which is not possible. You can do this by using malloc
as shown in another answer, but looking at your code, it's better to duplicate your call to fgets
, allowing you to move the definition of VLA outside your loop, something like:
if(fgets(line, sizeof(line), fd_in) != NULL) {
//get wordcount from line
int word[wordcount];
//put line data into array, using strtok()
int count = 1; // start from 1 per your question code, is it right?
while(fgets(line, sizeof(line), fd_in) != NULL) {
//need to access the array here
count++;
}
}