【问题标题】:Incompatible types when assigning to type 'char[100][100]' from type 'char *' error in 2d array从二维数组中的“char *”类型错误分配给“char [100] [100]”类型时的类型不兼容
【发布时间】:2021-05-10 19:49:57
【问题描述】:

我发现有char[100]; 的类似错误的问题,但他们没有帮助我。我只懂一些基本的C语言。

这是我的 C 代码,用于将人名和号码存储在二维数组中。

#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;
}

此程序显示 6 个错误和 2 个警告:

   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]'

我们将不胜感激。

【问题讨论】:

    标签: arrays c string struct typedef


    【解决方案1】:

    您的代码中有几个错误,如下所示:

    people[0].name = "Abu";
    

    首先,您不能像这样复制 C 风格的字符串。我们来看一个简单的例子:

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

    这里的错误是赋值的RHS计算为给定字符串文字的地址,而LHS是一个数组(不是可以赋值的类型)。 p>

    在这种情况下,您可以像这样使用strcpy 函数将数据从 RHS 复制到 LHS 的数组中:

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

    但是,在您的代码中,还有另一个问题:person 结构的 namenumber 字段被定义为 二维 字符数组(也就是说,每个都是100 个字符串的数组)。我看不出你想要这个的任何理由——大概每个人只有一个名字和一个数字(你在 people 数组中有多个 person 对象)。

    因此,我建议您重新定义 person 结构,使每个字段只有一个(100 个字符)字符串,如下所示:

    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;
    

    然后您可以使用如下行将数据复制到每个字段:

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

    注意:如果您确实要求每个人有多个姓名和号码,那么您需要在对strcpy 的调用中添加namenumber 字段的索引:

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

    请随时要求进一步澄清和/或解释。

    【讨论】:

    • 感谢您的回答,它帮助了我。但我已将字符串值分配给 2D 数组,如下所示“char names[100][100] = {"Abu","Simon"};"从此我可以通过 "printf("Name : %s",names[1]);" 打印名称不使用 strcpy,我怎么能在这里做到这一点。
    • 不使用 strcpy ...那么您将不得不单独复制每个字符,这实际上是编写您自己的 strcpy 版本。这就是 strcpy 的用途。
    • @TheophinJohnson 您似乎也将结构内部的“第二”维度与结构数组本身混淆了。你真的想要一个人有多个名字吗?
    猜你喜欢
    • 2021-03-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-01
    相关资源
    最近更新 更多