0

I need to access a variable length array i have created on the first line reading from a file. In order to access the array for when i am reading the following lines i would need to initialize it before line 1 is read out side of my conditional statement. but this is before I know the length of the array.

here is an example of my code

int count=0;
while (fgets(line, sizeof(line), fd_in) != NULL) {
  if(count==0){
    //get wordcount from line
    int word[wordcount];
    //put line data into array, using strtok()
  }else{
    //need to access the array here
  }
  count++;
}

Edit: My question is how should i go about being able to access this array where i need it?

Michael Kent
  • 383
  • 3
  • 17
  • Think: Is it possible to answer a question one does not know the answer for? – alk May 01 '14 at 13:22
  • theellipsis, you can't use VLA in such way. Use dynamic memory (`malloc`) and pointer to beginning of the array. Also you should not write to the array before it is allocated (declared). Store the info in other place. – osgx May 01 '14 at 13:24
  • Neither we nor the compiler is psychic, and it does not change the size of a VLA after it is in scope. How would it know if it can copy things around? – Deduplicator May 01 '14 at 13:25
  • 2
    If you do like in the example code shown in the question, then the variable `word` is local inside the scope where it is defined, and can only be accessed inside that scope. – Some programmer dude May 01 '14 at 13:26
  • I wasnt specifically asking how to do the impossible. More like are there any other methods such as what @osgx mentioned which i am about to look into. – Michael Kent May 01 '14 at 13:27
  • An XY-problem (http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem)? – alk May 01 '14 at 13:28
  • theellipsis, what is the format of your file? If the file is list of "words" and there is no info about their count, you should store read data in the growing array (malloc 10 elements, then if file is bigger, `realloc`-ing it to double size) and after the file ends and you have total counter, allocate right sized array and copy data to it. Or use linked lists. – osgx May 01 '14 at 13:29
  • I have re worded my question to avoid the xy problem – Michael Kent May 01 '14 at 13:36

2 Answers2

2

VLA arrays can't be accesses outside of the scope where they are declared (the scope is inside { } symbols).

So, if your fileformat has the total count in first line, you can use dynamic memory, and malloc your array:

int *words = NULL;
int count=0;
while (fgets(line, sizeof(line), fd_in) != NULL) {
  if(count==0){
    int wordcount = ...  //get wordcount from line
    //Allocate the memory for array:
    words = (int*) malloc( wordcount * sizeof(int) );
    //put line data into array, using strtok()
  }else{
    //need to access the array here
    words[count-1] = ....
  }
  count++;
}
osgx
  • 90,338
  • 53
  • 357
  • 513
  • thank you for your answer but i was getting a segmentation fault when i tried it, and the other solution seemed to work first time. – Michael Kent May 01 '14 at 15:18
2

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++;
    }
}
hyde
  • 60,639
  • 21
  • 115
  • 176