Cannot copy strings from pointer array with strcpy in C?

Viewed 1265

I am doing an exercise where a character pointer array is functioning as a way to store words. I do not understand why I cannot use 'strcpy' to copy the word 'hoi' to the second element of the array in the main function. When I compile the code I get the message 'program has stopped working' in CodeBlocks.

The functions 'numberOfWordsInDict' and 'printDict' are working properly.

Thanks in advance.

int numberOfWordsInDict(char **dict)
{
    int i, cnt = 0;
    for(i = 0; i < 10; i++)
    {
        if(dict[i] != NULL)
        {
            cnt++;
        }
    }
    return cnt;
}

void printDict(char **dict)
{
    int i = 0;
    printf("Dictionary:\n");
    if(numberOfWordsInDict(dict) == 0)
    {
        printf("The dictionary is empty.\n");
    } else
    {
        for(i = 0; i < 10; i++)
        {
            printf("- %s\n", dict[i]);
        }
    }
}

int main()
{
    char *dict[10] = {
            "aap", "bro ", "jojo", "koe", "kip", 
            "haha", "hond", "    drop", NULL,NULL};

    char *newWord1 = "hoi";
    printDict(dict);
    strcpy(dict[1], newWord1);
    printDict(dict);

    return 0;
}
5 Answers
Related