Can't copy strings from an array of pointers using strcpy in C?

I am doing an exercise in which an array of character pointers functions as a way to store words. I do not understand why I cannot use "strcpy" to copy the word "hoi" into the second element of the array in the main function. When I compile the code, I get a โ€œprogram stopped workingโ€ message in CodeBlocks.

The functions "numberOfWordsInDict" and "printDict" work correctly.

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;
}
+4
source share
5 answers

An array dictis an array of pointers to string literals.

strcpy(dict[1],newWord1);

.

C ++. undefined.

C (6.4.5 )

7 , , . , undefined.

, .

.

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

#define N   9

size_t numberOfWordsInDict( char dict[][N] )
{
    size_t n = 0;

    while ( *dict[n] ) ++n;

    return n;
}

void printDict( char dict[][N] )
{
    printf("Dictionary:\n");

    size_t n = numberOfWordsInDict( dict );
    if ( n == 0 )
    {
        printf("The dictionary is empty.\n");
    } 
    else
    {
        for ( size_t i = 0; i < n; i++ )
        {
            printf( "- %s\n", dict[i] );
        }
    }
}

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

    printDict(dict);
    strcpy(dict[1], newWord1);
    printDict(dict);

    return 0;
}

Dictionary:
- aap
- bro 
- jojo
- koe
- kip
- haha
- hond
-     drop
Dictionary:
- aap
- hoi
- jojo
- koe
- kip
- haha
- hond
-     drop

.

, C main โ€‹โ€‹

int main( void )
+2

"bro" , dict[1] , dict[1] .

strcpy(dict[1]), ..., , dict[1]. . .

.

+1

, , , "string" .

0

, .

0

RW . :)

#define MAXDICTSIZE 25

int numberOfWordsInDict(char dict[][MAXDICTSIZE], int dictsize)
{
    int i, cnt = 0;
    for (i = 0; i < dictsize; i++)
    {
        if (strlen(dict[i]))
        {
            cnt++;
        }
    }
    return cnt;
}

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

and somewhere in the main ()

char dict[10][MAXDICTSIZE] = {
    "aap", "bro ", "jojo", "koe", "kip",
    "haha", "hond", "    drop", "","" };

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

Source: https://habr.com/ru/post/1683439/


All Articles