[英]Why is there an error: incompatible types when assigning to type 'P_Name_t' from type 'UINT8 *'
I don't understand why I am getting the error above and how to fix it. 我不明白为什么我得到上面的错误以及如何解决它。 Please help.
请帮忙。
//STRUCTURES
typedef UINT8 P_Name_t[5];
typedef UINT8 ChipSN_t[3];
typedef struct
{
ChipSN_t ChipSN;
<other members>
} ChipIdent_t;
typedef struct Data_t
{
ChipIdent_t ReadOnlyMemID;
<other members>
} Data_t;
typedef struct
{
P_Name_t NameOfPart;
<other members>
} Log_t;
Data_t Data_Src;
typedef struct
{
P_Name_t NameOfPart;
ChipSN_t ChipSN;
}PartNum_ID_t;
//VARIABLE DECALARTION
PRIVATE PartNum_ID_t PN_ChipID[12];
PRIVATE Log_t *LogEntry = NULL;
//Usage in code
PN_ChipID[0].NameOfPart = LogEntry->NameOfPart;
PN_ChipID[0].ChipSN = Data_Src.ReadOnlyMemID.ChipSN;
I don't understand why I get the error. 我不明白为什么会收到错误消息。 The
LogEntry->NameOfPart
is of type P_Name_t
and PN_ChipID[0].NameOfPart
is of the same type. LogEntry->NameOfPart
的类型为P_Name_t
和PN_ChipID[0].NameOfPart
的类型相同。 I don't know if I am missing something here. 我不知道我是否在这里缺少任何东西。 Please explain.
请解释。
Apart from hiding it behind complicated structs and pointers, 除了将其隐藏在复杂的结构和指针之后,
you are basically trying to do this: 您基本上是在尝试这样做:
#include <string.h>
int main(void)
{
char a[5];
char b[5]={'a', 'b', 'c', 'd', 'e'};
// version 1 works
memcpy(a, b, sizeof(b)); // assuming that a and b are same size.
// version 2 gives the same error as you quote
//a=b;
return 0;
}
It gives the same error. 它给出了相同的错误。 While array identifiers used as R-values decay to pointers, you cannot "write to the pointer" representing an array as L-value.
虽然用作R值的数组标识符会衰减到指针,但是您不能“写入指针”将数组表示为L值。
Besides, what do you want to achieve? 此外,您想实现什么? What you anyway need to do is to copy the content of one array to the other.
无论如何,您要做的就是将一个数组的内容复制到另一个数组。 Use memcpy() or any other method to do that.
使用memcpy()或任何其他方法可以做到这一点。
incompatible types when assigning to type 'P_Name_t' [...]
[...] why i get the error[?] The
LogEntry->NameOfPart
is of typeP_Name_t
andPN_ChipID[0].NameOfPart
is of the same type.[...]为什么会出现错误[?]
LogEntry->NameOfPart
的类型为P_Name_t
和PN_ChipID[0].NameOfPart
的类型相同。
P_Name_t
is an array, namely of 5 unit8_t
. P_Name_t
是一个数组,即5 unit8_t
。
PN_ChipID[0].NameOfPart
is of type P_Name_t
. PN_ChipID[0].NameOfPart
的类型为P_Name_t
。
So this line 所以这条线
PN_ChipID[0].NameOfPart = LogEntry->NameOfPart;
tries to assign to an array, namely to PN_ChipID[0].NameOfPart
. 尝试分配给一个数组,即
PN_ChipID[0].NameOfPart
。
In C it is not possible to assign to an array, hence the error during compilation of the above line. 在C语言中, 无法分配给数组,因此在编译上述行时出错。
For the benefit of this thread and future answer seekers. 为了这个话题和将来的寻找者的利益。
LogEntry->NameOfPart
is an array of 5 elements, namely: LogEntry->NameOfPart
是由5个元素组成的数组,即:
LogEntry->NameOfPart[0]
LogEntry->NameOfPart[1]
LogEntry->NameOfPart[2]
LogEntry->NameOfPart[3]
LogEntry->NameOfPart[4]
and 和
PN_ChipID[0].NameOfPart
is similar PN_ChipID[0].NameOfPart
相似
PN_ChipID[0].NameOfPart[0]
PN_ChipID[0].NameOfPart[1]
PN_ChipID[0].NameOfPart[2]
PN_ChipID[0].NameOfPart[3]
PN_ChipID[0].NameOfPart[4]
So the way to copy this is to run it through a loop copying element by element 因此,复制此内容的方法是逐个循环复制
for i=....
{
PN_ChipID[0].NameOfPart[i] = LogEntry->NameOfPart[i];
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.