Reputation: 8717
Is it possible to declare a 2D array like so:
char words[128][25];
And then only assign to it like so:
int i=0;
while(fscanf(fp,"%s" words[i++]) != EOF){
printf("Copied");
}
I've seen it in a snippet of code, didn't understand so tried it out and just get a crash from codeblocks when it attempts to run this line. Is it a completely invalid way of using the array when it has been declared like that? It compiles fine it just doesn't run.
Upvotes: 0
Views: 150
Reputation: 1804
As well as what raptor explained, you also loop while you are equal to EOF.. this means you will never get the "Copied", UNLESS the file is empty.. You probably mean:
while(fscanf(fp,"%s",words[i++]) != EOF){
printf("Copied");
}
Referring to Whoz comment, it is true that this is a dangerous code, if your code has more lines than initialized in words, then you will get an exception, other than that, i indeed will state the number of lines(or words) extracted(-1)
Upvotes: 2
Reputation: 507
That is a legal way of using arrays; the problem appears to be in your while:
while(fscanf(fp,"%s words[i++]) == EOF){
You just need a closing quote and comma after the "%s
.
Upvotes: 2