-4

My professor gave me a C file and I can't really understand it well. It's a program that reads from a text file.

#include <stdio.h>
main(){
    int n,m;
    char start[30],end[30];
    FILE *a;
    a=fopen("test.txt","r");
    fscanf(a,"%s",&start);
    fscanf(a,"%s",&end);
    fscanf(a,"%d",&m);
    fscanf(a,"%d",&n);
    printf("\n%s\n%s\n%d\n%d",start,end,m,n);
    char word[n][30];
    int i=0;
    while(!feof(a)){
        fscanf(a,"%s",&word[i]);
        i++;

    }

    for(i=0;i<n;i++)
        printf("\n%s",word[i]);
    fclose (a);

}

here's the text file:

blah alooa
7 4
hey
boom
stackoverflow
testing

So, I'm wondering what does this char word[n][30] mean? why does it have [n]?

Jabberwocky
  • 48,281
  • 17
  • 65
  • 115
MareGraphics MaRe
  • 439
  • 1
  • 5
  • 14

2 Answers2

2

So, I'm wondering what does this char word[n][30] mean?

Technically, it is a 2 dimensional VLA of characters. It is used in this program to store "n" words, where each word can be of max 30 characters length.

why does it have [n]?

It's a variable used to store the number of words in the file.

Looking at your program, probably, you have to enter 8 as the value of n.

Mazhar
  • 575
  • 5
  • 20
-1

Well n is the number if word you have and it is readed from the file. It’s like you have some parameters stored within your file in the first lines

Amie Joni
  • 45
  • 1
  • 5