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.

Recommended Answers

All 5 Replies

>>word = (word*)malloc(num * sizeof(word));

You can't malloc() a non-pointer variable. Change line 49 to char **word; then change line 60 to word = malloc(num * sizeof(char *)); You will have to redesign other parts of that code too. The loop that appears before the malloc() will not work after changing word to a pointer.

Ok, so now I have

61:	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] = malloc(LENGTH * sizeof(word));
		if(word[i] == NULL)
		{
			fprintf(stderr, "Error getting space for strings\n");
			exit(-3);
75:		}
76:	}

However now I get a different error
words.c:61: error: incompatible types in assignment
words.c:70: error: incompatible types in assignment

re-read my previous post.

ok so

49:   char word[num][LENGTH];
50:
	for ( i = 0 ; i < num ; i++ )
	{
		fgets(word[i], LENGTH, ifp);
	}

was changed into

49:	char **word;
50:	
51:	**word = *line;

but I got a segmentation fault when I tried to run it.

then I changed the code to

#define SIZE 999
char line[SIZE];

48:	char **word;
	
50:	for( i = 0 ; i < num ; i++)
	{
		fgets(line, LENGTH, ifp) != NULL;
		**word = *line;
54:	}

but then I got
words.c:52: warning: value computed is not used

I added in 'line' to both segments thinking it would help. However both segments conclude with a segmentation fault.
Am I missing something? Pointers are not my string suit.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.