错误:不兼容的类型分配给键入时'的char [25]'从类型'字符*' [英] Error: incompatible types when assigning to type ‘char[25]’ from type ‘char *’
问题描述
我试图分配结构到地图的价值,但在编译后会出现以下错误信息:
错误:不兼容的类型分配给键入时'的char [25]'从类型'字符*'
在
地图[I] .N = M.N
我的结构的定义是这样的:
结构M1 {
INT℃;
CHAR N [25];
INT q_m;
INT q;};
我的code的部分:结果
结构M1米;
结构M1 *地图= 0;
scanf函数(%d个,&安培; M.C);
scanf函数(%S,&安培; M.N);
scanf函数(%d个,&安培; m.q_m);
scanf函数(%d个,&安培; m.q); 地图[I] .C = M.C;
地图[I] .N = M.N;
地图[I] .q_m = m.q_m;
地图[I] .Q = m.q;
阵列前pressions可能不是一个赋值的目标;在 =
运营商没有定义一个数组的内容的复制到其他。
如果 N
意味着持有0结尾的字符串,用的strcpy
:
的strcpy(图[I] .N,M.N);
如果 N
是为了举行一个非0结尾的字符串(或字符嵌入0值的序列),可以使用的memcpy
:
的memcpy(图[I] .N,M.N,sizeof的映射[I] .N);
除非它是的sizeof
或一元&放大器的操作;
运营商,或者是一个字符串使用初始化在声明中另一个数组,键入 T
N个元素的数组的前pression将被转换(衰变),以一个前$ p类型$ pssion指针 T
,和前pression的值将是第一个元素的地址。
这就是为什么你得到了你做了错误信息;恩pression M.N
有型字符
25个元素的数组因为它不是的操作数的的sizeof
或一元&安培;
运营商,它被转换为类型的char *
。 地图[I] .N
未转换(它停留键入的char [25]
),但正如我所说此前,前阵pressions可能不是赋值运算符的目标。
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;
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
:
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
:
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.
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.
这篇关于错误:不兼容的类型分配给键入时'的char [25]'从类型'字符*'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!