如何将c++数组从一个结构体复制到另一个结构体



我想把数组的一部分复制到另一个结构中。换句话说,我有一个程序(汽车制造商)接受结构A,另一个接受结构B (Labview),我需要以某种方式转换它们。

结构体A的转换——>struct B

#ifndef CONVERTERCPP2LV_ALGOBASETYPES_H
#define CONVERTERCPP2LV_ALGOBASETYPES_H
#include <cstdint>
namespace AlgoBaseTypes
{
typedef float float32;
typedef std::int32_t sint32;
typedef std::uint32_t uint32;
typedef std::int16_t sint16;
typedef std::uint16_t uint16;
typedef std::int8_t sint8;
typedef std::uint8_t uint8;
typedef std::uint64_t uint64;
typedef std::int64_t sint64;
typedef double float64;
typedef std::uint8_t boolean;
#define FALSE AlgoBaseTypes::boolean {0}
#define TRUE AlgoBaseTypes::boolean {1}
}
#endif //CONVERTERCPP2LV_ALGOBASETYPES_H

结构:

typedef struct ts_InnovizContextData
{
// firmware version as string
::AlgoBaseTypes::uint8 a_FirmwareVersion[20]{ 0 };
// Indicates if the Data is integral or corrupted
te_DataValidity E_DataValidity{ te_DataValidity::E_DataInvalid };
::AlgoBaseTypes::uint64 FrameCounter{ 0 };
::AlgoBaseTypes::uint64 TimestampMeasureStart{ 0 };
// unique sensor ID
::AlgoBaseTypes::uint32 SensorId{ 0 };
// The meaning of values of LidarState is defined in the following document (Chapter 6.1) 
::AlgoBaseTypes::uint8 LidarState{ 0 };     
::AlgoBaseTypes::uint8 LidarSubState{ 0 };
// number of all Lidar detections in each LRF
::AlgoBaseTypes::uint32 a_NumDetections[4]{ 0 };
} ts_InnovizContextData;
typedef struct ts_InnovizDetectionData
{
ts_InnovizContextData s_ContextData;
} ts_InnovizDetectionData;

Struct B:

typedef struct {
int32_t dimSize;
uint8_t* a_FirmwareVersion;
} TD5;
typedef TD5 **TD5Hdl;
typedef struct {
uint64_t FrameCounter;
uint64_t TimestampMeasureStart;
uint32_t SensorId;
uint8_t LidarState;
uint8_t LidarSubState;
TD4Hdl a_NumDetections;
TD5Hdl a_FirmwareVersion;
uint16_t te_DataValidity;
} TD3;
typedef struct {
//TD2 ts_InterfaceVersion;
TD3 ts_InnovizContextData;
//TD6 ts_Detection;
} TD1;

我第一次尝试使用memcpy

int main() {
//Struct A
InnovizDetectionData::ts_InnovizDetectionData Carmaker;
//Struct B
TD1 Labview;

//Try1: does not work for arrays --> NULL as reference
memcpy(&Labview, &Carmaker, sizeof(Carmaker));
//improved try:works for simple structures
memcpy(&Labview, &Carmaker, 22); // copy only 22 bytes
//uint8_t arrayToCopy[20]={0};
Labview.ts_InnovizContextData.a_FirmwareVersion.a_FirmwareVersion= uint_8[20]{0};
Labview.ts_InnovizContextData.a_FirmwareVersion.dimSize = 20;
//memcpy(&Labview.ts_InnovizContextData.a_NumDetections, &Carmaker.s_ContextData.a_NumDetections, sizeof(Carmaker.s_ContextData.a_NumDetections));
//memcpy(&Labview.ts_InnovizContextData.a_FirmwareVersion.a_FirmwareVersion , &Carmaker.s_ContextData.a_FirmwareVersion, sizeof(Carmaker.s_ContextData.a_FirmwareVersion) * 20);
return 0;
}

我可能只是创建一个结构体B的实例,然后手动遍历结构体B的每个成员,并从结构体A中分配等价的值(如果值类型不同,请记住值强制转换)。

你需要为B分配数组成员(a_FirmwareVersion和a_NumDetections),然后memcpy或loop through,因为它们是数组。

注意结构A中的a_FirmwareVersion的大小固定为20,而结构B的大小随dimSize而变化。对于A ->B转换后的dimSize总是变为20。如果反过来转换,则需要在第20个字节处停止。

然后将其包装成一个函数,该函数以结构体a作为输入并返回结构体b

如果需要,为B ->

使用std::arraystd::vector,您可以只分配值。您还将获得内存管理和免费移动语义。