Reputation: 439
I am coding a program which takes a text file as an input, makes the index of the words of it and prints the output(the index) in a file and in the screen.
the input file may be huge. but we KNOW that the maximum variety of the words used in the text file is 200. we don't know what's the maximum of lines and characters of each word. so I should reserve a large number for them. I took the maximum of line 1000 and the maximum characters of each word 100.
I am programming in Turbo C and (I am forced to use that). the compiler allocates just 64kb memory (with the size of the compiler included) and so I have to use MALLOC.
my program is supposed to work in this algorithm: it reads the input file line by line with fgets. then in the current line, it reads word by word with strtok. so far I have the xth word in yth line. I want to put the words in an array of pointers. so I need a char * word[200]. and I want to show how many times, which word is repeated in which line. so I need a int index [200][1000]. if in yth line, the xth word is existed I would do index[x][y]++.
so now I need to allocate MALLOC memory to these char * word[200] and int index[200][1000] . Can anyone help? I tried all the answers to such questions and none of them helped. see errors:
char **words;
words = malloc(sizeof(char*) * 200);//cannot conver void * to char **
for(int i = 0; i < 200; i++)
words[i] = malloc(100);//cannot convert void * to char *
int **index;
index = malloc(sizeof(int*) * 200)//cannot convert void * to int **
for (i = 0; i < 200; i++)
index[i] = malloc(sizeof(int) * 1000);
Upvotes: 0
Views: 318
Reputation: 40625
Your compiler does not conform to the C standard: In C you are allowed to implicitely convert any void*
to any other pointer (the opposite is also true). So the line
int* foo = malloc(sizeof(*foo));
is perfectly valid C code.
Your compiler, however, expects the following:
int* foo = (int*)malloc(sizeof(*foo));
to tell it that you really want to convert a void*
to an int*
. You can fix your code in an analog way.
Sidenote: It's good to remember this, because C++ has the same problem as your compiler (with the only difference that in C++ it is the standard that overly picky). So in C++ the first variant is illegal as well.
Upvotes: 1
Reputation: 5664
The malloc
function returns a void *
object, which you can think of as "a pointer to an undefined type". It's C's way of formatting a pointer when you don't know what kind of object it points to.
The errors Turbo C is issuing mean that you're not allowed to assign a void *
to a different kind of pointer (e.g. a char **
variable or int *
) without casting it explicitly to that type. You can do that by writing the malloc calls this way:
char **words;
words = (char **) malloc(sizeof(char*) * 200);
for(int i = 0; i < 200; i++)
words[i] = (char *) malloc(100);
int **index;
index = (int **) malloc(sizeof(int*) * 200);
for (i = 0; i < 200; i++)
index[i] = (int *) malloc(sizeof(int) * 1000);
Casting the result from malloc
is very unpopular these days, but if you have to use a compiler that considers implicit pointer casting to be an error, then the hell with that. :-)
Upvotes: 2