错误:当为类型“char *”指定类型“char[25]”时,不兼容的类型

时间:2021-01-05 19:21:02

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;

7 个解决方案

#1


8  

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

数组表达式可能不是赋值的目标;不定义=操作符将一个数组的内容复制到另一个数组中。

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

如果n的意思是持有一个0终止的字符串,那么使用strcpy:

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

If n is 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 sizeof or 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.

除非是运算符的操作数或一元&操作符,或者是一个字符串文字被用来初始化一个数组的声明,一个“T n元数组”类型的表达式将转换(“衰变”)一个“T”指针类型的表达式,表达式的值将是第一个元素的地址。

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

这就是为什么你得到了错误信息;表达式。n有“25元数组char”;由于它不是sizeof或unary &运算符的操作数,它被转换成char *。地图[我]。n没有被转换(它保持类型char[25]),但是正如我前面所说的,数组表达式可能不是赋值操作符的目标。

#2


3  

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

数组变量不能是赋值操作符的lvalue,也就是说它们不能被赋值。

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);

#3


1  

First you need to allocate memory for map.

首先需要为map分配内存。

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

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

并使用strcpyto复制m。n - >映射。

#4


1  

struct m1 *map;

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

地图是一个指针,你应该在给它写东西之前给它分配内存。

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

Then you can have

然后你可以有

map[i]

After this fix string copy

在此修复后的字符串复制。

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

#5


1  

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.

看起来您最可能想做的事情(除了为struct分配内存)正在复制由n指向的数组的内容,而不是只复制实际的指针。

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

#6


0  

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

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

map[i]=m;

#7


0  

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

看起来你想直接分配m。n值到数组。请看下面的详细例子:

#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

#1


8  

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

数组表达式可能不是赋值的目标;不定义=操作符将一个数组的内容复制到另一个数组中。

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

如果n的意思是持有一个0终止的字符串,那么使用strcpy:

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

If n is 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 sizeof or 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.

除非是运算符的操作数或一元&操作符,或者是一个字符串文字被用来初始化一个数组的声明,一个“T n元数组”类型的表达式将转换(“衰变”)一个“T”指针类型的表达式,表达式的值将是第一个元素的地址。

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

这就是为什么你得到了错误信息;表达式。n有“25元数组char”;由于它不是sizeof或unary &运算符的操作数,它被转换成char *。地图[我]。n没有被转换(它保持类型char[25]),但是正如我前面所说的,数组表达式可能不是赋值操作符的目标。

#2


3  

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

数组变量不能是赋值操作符的lvalue,也就是说它们不能被赋值。

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);

#3


1  

First you need to allocate memory for map.

首先需要为map分配内存。

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

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

并使用strcpyto复制m。n - >映射。

#4


1  

struct m1 *map;

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

地图是一个指针,你应该在给它写东西之前给它分配内存。

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

Then you can have

然后你可以有

map[i]

After this fix string copy

在此修复后的字符串复制。

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

#5


1  

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.

看起来您最可能想做的事情(除了为struct分配内存)正在复制由n指向的数组的内容,而不是只复制实际的指针。

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

#6


0  

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

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

map[i]=m;

#7


0  

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

看起来你想直接分配m。n值到数组。请看下面的详细例子:

#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