Dynamic array of structures with a dynamic arrays inside of it
Hi everyone!
So what is I'm supposed to do, is kind of a index program, which works, for example, like that:
./a.out FILE word1 word2 word3...
it should show:
word1 accused in lines 1 2 3
word 2 acussed in lines 4, 5, 6
etc..
but I've got some problem with structures that keeps informations about words;
heres the part of program code and a test function for it
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct {
char *word; // string contains a word
int how_many; // how many times word appears in file
int *numery; // in which lines word appers
} storage;
storage* init( int argc, char **argv)
{
storage **words; // thats is our dynamic array of structures //
int i;
for( i = 0; i < argc - 1; i++ )
{
words[i] = malloc( sizeof(storage) );
}
for( i = 0; i < argc - 1 ; i++)
{
words[i]->word = malloc( strlen( argv[i+1] ) * sizeof words[i]->word );
words[i] = realloc(words[i], sizeof(storage));
strcpy( words[i]->word, argv[i+1] );
printf( "\n%s\n", words[i] -> word );
}
return *words;
}
int main(int argc, char **argv ){
int i;
storage **words;
*words = init( argc, argv );
for( i = 0; i < argc - 1 ; i++ )
{
printf("\n%s\n", words[i] -> word );
}
return 0;
}
it looks like something is messed with malloc functions, and im locating memory unproperly..
please help, i dont know where to looking for bugs
thanks and sorry for my bad english