C语言 错误:从类型“char *”分配给类型“char[25]”时类型不兼容

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27707689/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 11:39:46  来源:igfitidea点击:

Error: incompatible types when assigning to type ‘char[25]’ from type ‘char *’

c

提问by Zombie

I'm trying to assign the values of a struct to a map but the following error message appears after compiling:

我正在尝试将结构的值分配给映射,但编译后出现以下错误消息:

error: incompatible types when assigning to type ‘char[25]' from type ‘char *' 

in

map[i].n=m.n

My struct is defined this way:

我的结构是这样定义的:

struct m1{
int c;
char n[25];
int q_m;
int q;};

Part of my code:

我的部分代码:

    struct m1 m;
    struct m1 *map = 0;
    scanf("%d",&m.c);
    scanf("%s",&m.n);
    scanf("%d",&m.q_m);
    scanf("%d",&m.q);

    map[i].c=m.c;
    map[i].n=m.n;
    map[i].q_m=m.q_m;
    map[i].q=m.q;

回答by John Bode

Array expressions may not be the target of an assignment; the =operator isn't defined to copy the contentsof one array to the other.

数组表达式可能不是赋值的目标;在=操作者没有定义的复制内容一个阵列的另一个。

If nis meant to hold a 0-terminated string, use strcpy:

如果n要保存以 0 结尾的字符串,请使用strcpy

strcpy( map[i].n, m.n );

If nis meant to hold a non-0-terminated string (or a sequence of characters with embedded 0 values), use memcpy:

如果n要保存非 0 终止的字符串(或带有嵌入 0 值的字符序列),请使用memcpy

memcpy( map[i].n, m.n, sizeof map[i].n );

Unless it is the operand of the sizeofor unary &operators, or is a string literal being used to initialize another array in a declaration, an expression of type "N-element array of T" will be converted ("decay") to an expression of type "pointer to T", and the value of the expression will be the address of the first element.

除非它是sizeof或 一元运算&符的操作数,或者是用于在声明中初始化另一个数组的字符串文字,否则“N 元素数组T”类型的表达式将被转换(“衰减”)为类型的表达式“指向”的指针T,表达式的值将是第一个元素的地址。

That's why you got the error message you did; the expression m.nhas type "25-element array of char"; since it wasn't the operand of the sizeofor unary &operators, it was converted to type char *. map[i].nwasn't converted (it stayed type char [25]), but as I said earlier, array expressions may not be the target of the assignment operator.

这就是为什么您收到错误消息的原因;表达式的m.n类型为“25 个元素的数组char”;因为它不是 thesizeof或 unary 运算&符的操作数,所以它被转换为 type char *map[i].n没有转换(它保持 type char [25]),但正如我之前所说,数组表达式可能不是赋值运算符的目标。

回答by alk

Array variables cannot be an lvalueto the assignment operator, that is they cannot be assigned anything.

数组变量不能是赋值运算符的左值,也就是说它们不能被赋值。

To copy an array, copy element by element or use a "low-level" function like memcpy()to copy a specific amount of memory at once:

要复制数组,请逐个复制元素或使用“低级”函数,例如memcpy()一次复制特定数量的内存:

memcpy(map[i].n, m.n, sizeof map[i].n);

回答by KRUPASINDHU MOHANTY

Looks like you are trying to assign directly m.n value to the array. Please see below detail Example :

看起来您正在尝试将 mn 值直接分配给数组。请参阅下面的详细示例:

#include<stdio.h>
#include<stdlib.h>

struct db{
    int db_num;
    char db_name[10];
};

int main()
{
    struct db *ptr;
    ptr = (struct db*)malloc(sizeof(struct db));
    ptr->db_num = 10;
    ptr->db_name = "xyz";
    printf("Input data Base:\n");
    printf("db_num:%d db_name:%s",ptr->db_num,(char*)ptr->db_name);
    return 0;
}

In the above code snippet I am trying to assign "XYZ" to the array which is the member of struct db. It through the similar Error because of ptr->db_name = "xyz";

在上面的代码片段中,我试图将“XYZ”分配给作为 struct db 成员的数组。它通过类似的错误,因为 ptr->db_name = "xyz";

st_dyna_mem.c:14: error: incompatible types when assigning to type ‘char[10]' from type ‘char *'

st_dyna_mem.c:14: 错误:从类型 'char *' 分配到类型 'char[10]' 时类型不兼容

Fix : For Fixing this type of issue you Can use strcpy() or memcpy(). EX:

修复:要修复此类问题,您可以使用 strcpy() 或 memcpy()。前任:

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

struct db{
    int db_num;
    char db_name[10];
};

int main()
{
    struct db *ptr;
    ptr = (struct db*)malloc(sizeof(struct db));
    ptr->db_num = 10;
    strcpy(ptr->db_name,"xyz");
    printf("Input data Base:\n");
    printf("db_num:%d db_name:%s",ptr->db_num,(char*)ptr->db_name);
    return 0;
}

Output: db_num:10 db_name:xyz

输出:db_num:10 db_name:xyz

回答by haccks

First you need to allocate memory for map.

首先,您需要为map.

struct m1 *map = malloc(sizeof(struct m1)); 

and use strcpyto copy m.nto map->n.

并用于strcpy复制m.nmap->n.

回答by Gopi

struct m1 *map;

mapis a pointer and you should be allocating memory to it before writing something to it.

map是一个指针,您应该在向其写入内容之前为其分配内存。

map = malloc(sizeof(struct m1) * n);

Then you can have

然后你可以有

map[i]

After this fix string copy

在此修复字符串复制之后

strcpy(map[i].n,m.n);

回答by Tommy Andersen

It seems like what you most likely want to do (besides allocating memory for the struct) is copying the contents of the array pointed to by n, instead of only copying the actual pointer.

看起来您最有可能想要做的(除了为结构分配内存)是复制 指向的数组的内容n,而不是仅复制实际的指针。

strcpy(map[i].n, m.n);

回答by Hans Klünder

You copy all of the structure members. The simplest way to do that is:

您复制所有结构成员。最简单的方法是:

map[i]=m;