Cに関する私の質問に関連するいくつかの投稿を読みました.これは確かに私のエラーを減らすのに役立ちます. ただし、他の投稿では解決できなかった問題がまだ残っています。基本的に、これは私がやろうとしていることです。
main で配列を定義します。この配列へのポインターを関数に渡します。この関数は、ファイルを開き、そのファイルを解析し、そのファイルからの情報を、渡したポインターの配列に入れます。まあ、失敗します。
私が得るエラーは次のとおりです。
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
コード全体は以下のとおりです。ただし、配列やポインターなどをどのように定義したかに注目するだけでよいと思います。
#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);
}