I have read several posts related to my question on C. This indeed help me cut down on my errors. However, I'm still having problems that other posts couldn't solve for me. Basically, this is what I am trying to do.
Define an array in main. I pass in a pointer to this array to a function. This function will open up a file, parse that file, and put information from that file into the array whose pointer I passed in. Well, it fails.
The errors I get are:
work.c:12: error: array type has incomplete element type
work.c: In function ‘main’:
work.c:20: error: type of formal parameter 1 is incomplete
work.c: At top level:
work.c:25: error: array type has incomplete element type
The entire code follows below. But I think you only need to focus on how I defined my array, and pointer, and so on.
#include <stdio.h>
#include <stdlib.h>
//Defining Preprocessed Functions
char readFile(char array[][], int, int);
//void displayStudentInfo(int*);
//Implements Step 1 and 2 of Student Instuctions
int main(int argc, char* argv[])
{
int x = 256;
int y = 256;
char arrays[x][y];
readFile(&arrays,x,y);
//displayStudentInfo(&array);
return 0;
}
char readFile(char array[][], int x, int y)
{
char line[256]; //Three columns 0, 1, 2 corresponds to firstname, lastname, score.
char* string;
int columns = 3;
x = 0;
//int y; //Defines rows and columns of 2D array to store data
//char array[x][y]; //Defines the array which stores firstname, lastname, and score
FILE *file;
file = fopen("input.txt", "r");
//Test to make sure file can open
if(file == NULL)
{
printf("Error: Cannot open file.\n");
exit(1);
}
else
{
while(!feof(file))
{
/*
if(fgets(line, 256, file))//fgets read up to num-1 characters from stream and stores them in line
{
printf("%s", line);
}
*/
if(fgets(line,256,file)!=NULL)
{
for(y = 0; y < columns; y++)
{
array[x][y]=strtok(fgets(line,256,file), " ");
}
x++;
}
}
}
fclose(file);
}