-1

I want to create function that adds words into dictionary

so far i made this

void addWord(char **dictionary,int *dictionarySize,int *wordsInDictionary,char *word){
if(dictionary == NULL)
{
    *dictionary = (char *)malloc(sizeof(char*)*(*dictionarySize));
}
else
{
    if(*wordsInDictionary==*dictionarySize)
    {
        *dictionary = (char *)realloc(dictionary,sizeof(char*)*(*dictionarySize)*2);
        (*dictionarySize)*=2;
    }
}
dictionary[*wordsInDictionary]=word;
(*wordsInDictionary)++;

}

in main() i have

int i;
int dictSize = 1;
int wordsInDict = 0;
char *word;
char *dictionary;
dictionary=NULL;

then i want to print all words in dictionary , but here i get warning that %s is expecting char* but it is int

printf("Stats: dictsize: %d, words in dict: %d\n", dictSize,wordsInDict);
    for(i=0;i<wordsInDict;i++)
    {
        printf("%d. %s\n",i, dictionary[i]);
    }

it also gives me errors when i try to add words

i use this call to add words

addWord(&dictionary,&dictSize,&wordsInDict,word);
lllook
  • 729
  • 2
  • 7
  • 20

2 Answers2

1

In your addWord function, dictionary will never be NULL.

And that's only the start of your problems. Because you want dictionary to be an array of arrays, which mean you need to declare it as a pointer to a pointer (if you want it to be dynamic). However, you declare it as just a (single) pointer. It's in the main function (or where ever you declare it originally) that you need to declare it as a pointer to a pointer. And you need to initialize it, or it will have an indeterminate value and using it in any way other than initializing it will lead to undefined behavior.

That means your addWord function should take a pointer to a pointer to a pointer, i.e. one more level of indirection. And you need to use the dereference operator to get the original pointer to pointer.

So the addWord function should start like e.g.

void addWord(char ***dictionary, int *dictionarySize, int *wordsInDictionary,char *word){
    if(*dictionary == NULL)
    {
        *dictionary = malloc(sizeof(char*) * (*dictionarySize));
    }
    ...
}

Also note that I don't cast the return of malloc.

Also note that realloc can fail, and then will return NULL, so if you assign the return to the same pointer you reallocate you will loose the original pointer. Always use a temporary pointer for the return-value of realloc and only assign to the real pointer after checking that the reallocation succeeded.

Community
  • 1
  • 1
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • what do you mean i want array of arrays? i want array of strings, do you view string as an array of characters? i know this im just not used to it, is there any better approach on this? for example without using ***dictionary which looks complicated to me – lllook Jan 17 '15 at 15:51
  • @lllook And what is a string? It is, basically, an *array* of characters. And if you want an array of strings, you want an array of arrays, and if you want to allocate those dynamically, then you need a pointer to a pointer. – Some programmer dude Jan 17 '15 at 15:56
  • 1
    @lllook Also be careful with how you use `word`, if you reuse it to read multiple words then all entries will be equal to the last word because all entries use the same pointer. You might want to use e.g. `strdup` to duplicate the strings (but if you do then you have to remember to free the actual strings as well). – Some programmer dude Jan 17 '15 at 15:58
0

I suggest that you put together the members of the dictionary in one as a structure, rather than having individually.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct dictionary {
    char **words;//array of char *
    int size;
    int numOfWords;
} Dictionary;

Dictionary *D_new(void){
    Dictionary *dic = malloc(sizeof(*dic));
    if(dic){
        dic->size = 16;//initial size
        dic->words = malloc(dic->size * sizeof(*dic->words));//check omitted
        dic->numOfWords = 0;
    }
    return dic;
}
void D_drop(Dictionary *dic){
    int i;
    for(i=0;i<dic->numOfWords; ++i)
        free(dic->words[i]);
    free(dic->words);
    free(dic);
}
void addWord(Dictionary *dic, const char *word){
    if(dic == NULL){
        return ;
    }
    if(dic->numOfWords == dic->size){
        dic->words = realloc(dic->words, sizeof(*dic->words)*(dic->size*=2));//check omitted
    }
    dic->words[dic->numOfWords++]=strdup(word);//make copy
}

int main(void){
    int i;
    Dictionary *dictionary = D_new();
    addWord(dictionary, "apple");
    addWord(dictionary, "banana");
    addWord(dictionary, "melon");

    printf("Stats: dictsize: %d, words in dict: %d\n", 
    dictionary->size, dictionary->numOfWords);
    for(i=0;i<dictionary->numOfWords;i++){
        printf("%d. %s\n", i, dictionary->words[i]);
    }

    D_drop(dictionary);
    return 0;
}
BLUEPIXY
  • 39,699
  • 7
  • 33
  • 70