Hello, I am currently working on a program which will take a set of strings from a data file and store them into an array called word.
So far my code has compiled correctly however I just added in malloc and it just doesn't like me. I need to malloc the array and the individual
words.c:62: error: expected expression before ')' token
words.c:71: error: expected expression before ')' token
#include <stdio.h>
#include <stdlib.h>
#define LENGTH 49
int num = 14;
49: char word[num][LENGTH];
50:
for ( i = 0 ; i < num ; i++ )
{
fgets(word[i], LENGTH, ifp);
}
55:
rewind(ifp);
/* Close the file since we're finished reading from it */
fclose(ifp);
60:
word = (word*)malloc(num * sizeof(word));
if(word == NULL)
{
fprintf(stderr, "Error getting space for strings\n");
65: exit(-3);
}
for(i = 0; i < num; i++)
{
70: word[i] = (word*)malloc(LENGTH * sizeof(char));
if(word[i] == NULL)
{
fprintf(stderr, "Error getting space for strings\n");
exit(-3);
75: }
76: }
Also, this is in C language. I cant seem to figure out what I'm missing. Any help would be greatly appreciated. Also, you can assume that word already contains all the strings I need. This code would compile and run correctly before I added in malloc. Each element of word contains strings of varying character length, up to a max of 49 characters.