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?

8
  • Think: Is it possible to answer a question one does not know the answer for? Commented May 1, 2014 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. Commented May 1, 2014 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? Commented May 1, 2014 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. Commented May 1, 2014 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. Commented May 1, 2014 at 13:27

2 Answers 2

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++;
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

I marked this as the correct answer as it worked well for me upon first implementation.
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++;
}

1 Comment

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.

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.