PDA

View Full Version : fscanf & structs


Das Oracle
March 14th, 2006, 03:06 PM
Here is any relevant code:

typedef struct{
char* word;
int score;
}tword;

tword* dictionary=NULL;
fscanf(ifp,"%d",&size);
dictionary = malloc(size*sizeof(tword));
for(index=0;index<size;index++){
fscanf(ifp,"%s",dictionary[index].word);
}

the problem is that when i try to print the words out like this:

for(index=0;index<size;index++)
printf("%s\n",dictionary[index].word);

the output is null any ideas why?

pharcyde
March 14th, 2006, 03:20 PM
Does your stream actually have any input? Try using sscanf with simple buffer and see if you still have a problem.

LordHunter317
March 14th, 2006, 03:21 PM
You never allocate any space for the string, that's why.

Your allocation for the tword structure is incorrect. When you call:malloc(size * sizeof(tword))You allocate space for a char pointer and an integer size times. You don't allocate space for the string, just a pointer to it.

You need to allocate space for each of the strings as well.