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);
}
You have a few problems. The first two are similar:
First, you’re using an unbounded array in your function declaration: the compiler needs to know more about the argument, i.e. the dimensions. In this case it’s sufficient to provide one of the dimensions:
Now the compiler has enough information to handle the array. You can leave off a dimension like this, but it’s usually better to be explicit, and declare the function as:
Next, when you’re declaring your
arraysarray in main, you’ll need to be more specific about dimensions – similar to the argument list for the function:Choose
NUM_Yto be more than large enough fit the amount of data that you’ll expect.Next, you’re not initializing
xandyinmain, then you go on to declare an array using those variables. This is bad because these variables can contain any garbage value, including0, and so you’ll end up with an array of unexpected dimensions/size.Finally, when you pass the array to your function, don’t de-reference it, just pass along the variable:
In C, when you pass an array to a function, what is actually passed is a pointer to the first element. This means that the array isn’t copied, and so the function has access to the area of memory that it expects to change. I’ll guess that you’re de-referencing because that’s the way that you’ve learned to pass simpler types that you want to change in the function, like
intsorstructs, but with arrays you don’t need to do this.