简体   繁体   English

为什么会出现错误:从类型'UINT8 *'分配给类型'P_Name_t'时类型不兼容

[英]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_tPN_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 type P_Name_t and PN_ChipID[0].NameOfPart is of the same type. [...]为什么会出现错误[?] LogEntry->NameOfPart的类型为P_Name_tPN_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.

相关问题 分配给&#39;uint16_t&#39;类型错误时,为什么会得到不兼容的类型? - Why do I get an incompatible types when assigning to type 'uint16_t ' error? 从“ uint8_t”类型分配给“ typedef”类型时的类型不兼容 - Incompatible types when assigning to type ‘typedef from type ‘uint8_t’ 错误:从类型&#39;digestInfo&#39;分配给类型&#39;char *&#39;时类型不兼容 - error: incompatible types when assigning to type ‘char *’ from type ‘digestInfo’ 错误:从“void *”类型分配类型“值”时出现不兼容的类型? - Error: incompatible types when assigning to type 'values' from type 'void *'? 从C中的类型分配类型时出错类型不兼容 - error incompatible types when assigning to type from type in C 从“ int”类型分配给“ t_result”类型时,类型不兼容,为什么? - Incompatible types when assigning to type 't_result' from type 'int', why? 错误:分配和返回类型时类型不兼容 - Error: incompatible types when assigning to and returning to type 从“ int”类型分配给“ memstruct”类型时不兼容的类型 - incompatible types when assigning to type ‘memstruct’ from type ‘int’ 从“数组*”类型分配给“数组[256]”类型时不兼容的类型 - incompatible types when assigning to type 'array[256]' from type 'array *' 从“ int”类型分配给“ struct protein”类型时不兼容的类型 - incompatible types when assigning to type 'struct protein' from type 'int'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM