I want read a simple file line by line and save the content into an array. If I compile the source code I get this error:
test.C: In function ‘char* read_file(int, int, const char*)’:
test.C:14:11: error: cannot convert ‘char (*)[1024]’ to ‘char*’ in return
return words;
^
What does it means? Why can I not return a pointer of an 2D-Array? Here is the source code.
#include <stdio.h>
#include <string.h>
char * read_file(int buf, int size, const char *fname){
char words[size][buf];
FILE *fptr = NULL;
int idx = 0;
fptr = fopen(fname, "r");
while(fgets(words[idx], buf, fptr)) {
words[idx][strlen(words[idx]) - 1] = '\0';
idx++;
}
return words;
}
int main(void) {
char * words;
words = read_file(1024, 100, "text.txt");
int i;
for(i = 0; i < 100; ++i)
printf("%s\n", words[i]);
}