[英]Convert uint8_t array to structure in C
I'm beginner and I have a problem.我是初学者,我有一个问题。 I have this structure:
我有这个结构:
typedef struct {
char data[26];
int index;
Placa_baza pb; // char nume_placa[10], int index_placa;
} PC;
And a structure vector:和一个结构向量:
static PC computers[5] = { ... };
I need to have a vector of type uint8_t pc[5*sizeof(computers)]
instead of the structure vector.我需要一个
uint8_t pc[5*sizeof(computers)]
类型的向量而不是结构向量。 Is it well declared that way?这样声明好吗? :
:
uint8_t pc[5*sizeof(computers)]
How can I convert (cast) vector uint8_t pc[5*sizeof(computers)]
to PC
?如何将(转换)向量
uint8_t pc[5*sizeof(computers)]
转换为PC
?
To use the uint8_t
pointer to address the structure, how should it be written?要使用
uint8_t
指针来寻址结构,应该怎么写呢?
Thank you in advance.先感谢您。
Your pc
array, which could serve as a backup for the PC
data is too large: it is sufficient to define it as:您的可用作
PC
数据备份的pc
阵列太大:将其定义为:
uint8_t pc[sizeof(computers)];
Or possibly:或者可能:
uint8_t pc[5 * sizeof(PC)];
You can then copy computers
to pc
with:然后,您可以使用以下命令将
computers
复制到pc
:
memcpy(pc, computers, sizeof pc);
You could also use a pointer to access the pc
array as an array of PC
:您还可以使用指针来访问
pc
数组作为PC
数组:
PC *p = (PC *)pc; // Don't do this!
Note however that this has undefined behavior as the byte array pc
might not be properly aligned to access members of the PC
structure, especially the index
member and using such a pointer is a violation of the strict aliasing rule.但是请注意,这具有未定义的行为,因为字节数组
pc
可能未正确对齐以访问PC
结构的成员,尤其是index
成员,并且使用这样的指针违反了严格的别名规则。 It would be much better to define pc
as PC pc[5];
最好将
pc
定义为PC pc[5];
and access this array via a uint8_t
pointer of so required.并通过所需的
uint8_t
指针访问此数组。
WARNING: Below program is just demonstration purpose, it may not behave same way with all compilers/systems.警告:以下程序仅用于演示目的,它可能与所有编译器/系统的行为方式不同。 You can use it to test your compilers or systems behavior and modify accordingly.
您可以使用它来测试您的编译器或系统行为并进行相应的修改。
In the below program am copying the contents from the structure computers
to unit8_t
.在下面的程序中,我将结构
computers
中的内容复制到unit8_t
。
as you can see its not easy and not portable, because we need to extract the data as per the boundaries of memory, allocated for variables.如您所见,它不容易且不可移植,因为我们需要根据 memory 的边界提取数据,分配给变量。
#include <stdio.h>
#include <stdint.h>
#include <string.h>
typedef struct
{
char nume_placa[10];
int index_placa;
}Placa_baza;
typedef struct {
char data[26];
int index;
Placa_baza pb;
}PC;
int main()
{
printf("sizeof(int) = %zu\n", sizeof(int));
printf("sizeof(Placa_baza) = %zu\n", sizeof(Placa_baza));
printf("sizeof(PC) = %zu\n", sizeof(PC));
static PC computers[3] = { {"data1",1,"comp1", 0}, {"data2",2,"comp2", 1}, {"data3",3,"comp3", 2} };
printf("sizeof(computers) = %zu\n\n", sizeof(computers));
for(int i =0; i<3; i++)
printf("data = %s, index =%d, pb.nume_placa =%s, pb.index_placa =%d\n",
computers[i].data,
computers[i].index,
computers[i].pb.nume_placa,
computers[i].pb.index_placa
);
uint8_t uint8_t_pc[sizeof(computers)] = {0};
// for copying the contents from pc (uint8_t), used same variable names as that of structures
/* typedef struct { */
char data[26];
int index;
/* Placa_baza pb;
} PC; */
/* typedef struct
{ */
char nume_placa[10];
int index_placa;
/* }Placa_baza;
*/
printf("\n sizeof(uint8_t_pc) = %zu\n", sizeof(uint8_t_pc));
memcpy(uint8_t_pc,computers,sizeof(computers));
int count = 0;
uint8_t* temp = uint8_t_pc;
printf("\n **From uint8_t memory ***\n");
while(count < 3) {
memcpy(data, temp, 26);
// since there is a padding of 2 bytes , so extract from 28
memcpy(&index, temp+28, 4);
memcpy(nume_placa, temp+32, 10);
//again there is a padding of 2 bytes
memcpy(&index_placa, temp+44, 4);
printf("data = %s, index = %d, nume_placa =%s , index_placa =%d\n", data, index, nume_placa, index_placa);
temp = temp+sizeof(computers[0]);
count++;
}
return 0;
}
Output: Output:
sizeof(int) = 4
sizeof(Placa_baza) = 16
sizeof(PC) = 48
sizeof(computers) = 144
data = data1, index =1, pb.nume_placa =comp1, pb.index_placa =0
data = data2, index =2, pb.nume_placa =comp2, pb.index_placa =1
data = data3, index =3, pb.nume_placa =comp3, pb.index_placa =2
sizeof(uint8_t_pc) = 144
**From uint8_t memory ***
data = data1, index = 1, nume_placa =comp1 , index_placa =0
data = data2, index = 2, nume_placa =comp2 , index_placa =1
data = data3, index = 3, nume_placa =comp3 , index_placa =2
Update: Indeed we can use offsetof
to get the offset of any member of the structure, so the statements inside while
can also be replaced by below statments.更新:确实我们可以使用
offsetof
来获取结构中任何成员的偏移量,所以while
里面的语句也可以用下面的语句代替。
memcpy(data, temp+offsetof(PC, data), sizeof(computers[count].data));
memcpy(&index, temp+offsetof(PC, index), sizeof index);
memcpy(nume_placa, temp+offsetof(PC, pb.nume_placa), sizeof computers[count].pb.nume_placa);
memcpy(&index_placa, temp+offsetof(PC, pb.index_placa), sizeof index_placa);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.