I am having trouble assigning a char string to a c

2020-02-15 08:12发布

问题:

This is what I have now.

void insert(char Table[][81], char Key[81]){
    int index;
    index = search(Table, Key); 
    // This is another function used to find an empty 'slot'
    // in the table
    if(Table[index] == '\0')
    Table[index] = Key; 
    // <-- This is the line that contains some sort of error.
    else
    printf("ERROR: Key already in Table\n");
}

The error it is throwing is:

incompatible types when assigning to type 'char[81]' from type 'char *'.

I am at a loss as to how to fix or why this error is being thrown. If anyone needs more info from my program to draw a conclusion please let me know.

回答1:

You cannot assign arrays but you can use strcpy() or memcpy() instead:

if (Table[index][0] == '\0')
    memcpy(Table[index], Key, sizeof(Key));
else
    printf("ERROR: Key already in Table\n");