我不知道如何定义所有用户定义类型的好处,但您的直接问题似乎是您的 PROCESSING 结构以某种方式未完全定义。也许您缺少适当的头文件或其他内容的#include。一旦解决了这个问题,如果您需要有关编译器如何在内存中布局结构的更多信息,您可以创建一些临时调试代码,如下所示(请注意,我冒昧地添加了您的类型的定义,这可能不符合你自己的定义)....
typedef char PROCESSOR;
typedef char Status;
struct Time
{
char t[8];
};
struct PROCESSING
{
PROCESSOR Byte ;
uint16_t count ;
uint8_t overrange ;
Status status ;
Time timestamp ;
float Field;
float temperature ;
float adc1;
float dac1;
float Field2;
float Temperature;
float adc2 ;
float dac2 ;
};
int main()
{
PROCESSING p;
printf("Member Size Offset\n"
"-------------- ------ ------\n");
#define MACROS_MAKE_THE_WORLD_GO_AROUND(structMember) \
printf("%14s ", #structMember); \
printf(" %3d ", sizeof(p.structMember)); \
printf(" %3d\n", offsetof(PROCESSING,structMember));
MACROS_MAKE_THE_WORLD_GO_AROUND(Byte)
MACROS_MAKE_THE_WORLD_GO_AROUND(count)
MACROS_MAKE_THE_WORLD_GO_AROUND(overrange)
MACROS_MAKE_THE_WORLD_GO_AROUND(status)
MACROS_MAKE_THE_WORLD_GO_AROUND(timestamp)
MACROS_MAKE_THE_WORLD_GO_AROUND(Field)
MACROS_MAKE_THE_WORLD_GO_AROUND(temperature)
MACROS_MAKE_THE_WORLD_GO_AROUND(adc1)
MACROS_MAKE_THE_WORLD_GO_AROUND(dac1)
MACROS_MAKE_THE_WORLD_GO_AROUND(Field2)
MACROS_MAKE_THE_WORLD_GO_AROUND(Temperature)
MACROS_MAKE_THE_WORLD_GO_AROUND(adc2)
MACROS_MAKE_THE_WORLD_GO_AROUND(dac2)
#undef MACROS_MAKE_THE_WORLD_GO_AROUND
printf("\n Total size of PROCESSING struct: %d bytes\n\n", sizeof(p));
}
当你运行它时,你会看到这样的东西:
Member Size Offset
-------------- ------ ------
Byte 1 0
count 2 2
overrange 1 4
status 1 5
timestamp 8 6
Field 4 16
temperature 4 20
adc1 4 24
dac1 4 28
Field2 4 32
Temperature 4 36
adc2 4 40
dac2 4 44
Total size of PROCESSING struct: 48 bytes
一旦你有了这些,你就可以开始弄清楚你可能需要做什么来使你的结构与嵌入式系统的结构布局相匹配,这可能比简单地使结构具有相同的整体大小更复杂。但是其他人给出的#pragma 提示可能是一个好的开始。注意编译器添加的填充,您可以通过将“大小”列相加并将其与“偏移”列进行比较来计算,如果它们不匹配,您就知道编译器插入了一些填充。