Incompatible types when assigning to type 'char[100][100]' from type 'char *' error in 2d array

Viewed 283

I found questions for similar errors having char[100]; but they didn't help me. I only know some basic C language.

This is my C code to store a person's name and number in a 2D array.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct {
   char name[100][100];
   char number[100][100];
}
person;

int main(void) {
    setvbuf(stdout,NULL,_IONBF,0);
    setvbuf(stderr,NULL,_IONBF,0);
    person people[3];
    int i;
    people[0].name = "Abu";
    people[0].number = "+91-123456789";
    people[1].name = "Tony";
    people[1].number = "+1-4432788878";
    people[2].name = "Simon";
    people[2].number = "+42-432432233";
    for(i=0;i<=2;i++){
        if(strcmp(people[i].name,"Simon")==0){
            printf("Found %s\n",people[i].number);
            return 0;
        }
    }printf("Not found\n");
    return 1;
}

This program shows 6 error and 2 warnings:

   error: incompatible types when assigning to type 'char[100][100]' from type 'char *'
   error: incompatible types when assigning to type 'char[100][100]' from type 'char *'
   error: incompatible types when assigning to type 'char[100][100]' from type 'char *'
   error: incompatible types when assigning to type 'char[100][100]' from type 'char *'
   error: incompatible types when assigning to type 'char[100][100]' from type 'char *'
   error: incompatible types when assigning to type 'char[100][100]' from type 'char *'
   typedef struct sample.c:31:3: warning: implicit declaration of function 'strcmp'
   typedef struct sample.c:32:4: warning: format '%s' expects type 'char *',
   but argument 2 has type 'char (*)[100]'

Any help will be appreciated.

1 Answers

There are several errors in your code, in the lines like the following:

people[0].name = "Abu";

First, you cannot copy C-style character strings like this. Let's look at a simple example:

char dest[100]; // An array of 100 characters;
dest = "This is what I want in 'dest'."; // Error!

The error here is that the RHS of the assignment evaluates to the address of the given string literal and the LHS is an array (which is not a type that can be assigned to).

In this case, you use the strcpy function, like so, to copy the data from the RHS into the array of the LHS:

char dest[100]; // An array of 100 characters;
strcpy(dest,"This is what I want in 'dest'.");

However, in your code, there is another problem: The name and number fields of the person structure are defined as two dimensional arrays of characters (that is, each is an array of 100 strings). I can't see any reason why you would want this – presumably, each person has only one name and one number (and you have multiple person objects in the people array).

So, I would suggest that you redefine your person structure to have only a single (100 character) string for each field, like so:

typedef struct {
    char name[100];     // Allows up to 99 characters for each name and number...
    char number[100];   // ... 'reserving' one `char` for the REQUIRED nul terminator
} person;

Then you can copy your data to each field with lines like the following:

strcpy(people[0].name, "Abu");
strcpy(people[0].number, "+91-123456789");
strcpy(people[1].name, "Tony");
//... and so forth

Note: If you do actually require multiple names and numbers for each person, then you will need to add the index of the name or number field in the call to strcpy:

strcpy(people[0].number[0], "+91-123456789"); // Sets Abu's FIRST number
strcpy(people[0].number[1], "+91-987654321"); // Sets Abu's SECOND nmuber

Feel free to ask for further clarification and/or explanation.

Related