C++ 填充字符指针的结构

C++ 填充字符指针的结构,c++,C++,假设你有这样的结构: struct MMFS_IDENTIFICATION { char *szVendor; char *szControllerModel; char *szRevision; char *szId; char *szExecutive; char *szKarelRevision; char *szProcessName; char *szCommRevision; char *szRobotModel

假设你有这样的结构:

struct MMFS_IDENTIFICATION
{
    char *szVendor;
    char *szControllerModel;
    char *szRevision;
    char *szId;
    char *szExecutive;
    char *szKarelRevision;
    char *szProcessName;
    char *szCommRevision;
    char *szRobotModel;
};
有什么简单的方法可以做这样的事情吗

MMFS_IDENTIFICATION mmfsId;

for( int i = 0; i < 9; i++ )
{
    int len = buf[pos++];
    mmfsId[i] = malloc(len);
    memcpy( mmfsId[i], buf[pos], len );
    pos += len;
}
MMFS\u标识mmfsId;
对于(int i=0;i<9;i++)
{
int len=buf[pos++];
mmfsId[i]=malloc(len);
memcpy(mmfsId[i],buf[pos],len);
pos+=len;
}

我知道要做的唯一一件事就是把代码复制粘贴九次。但我真的不想这么做,因为在我使用这个概念的实际程序中,计算len并不像我在这个例子中做的那么简单

由于所有结构成员都是
char*
指针,因此可以执行以下操作:

#include <pshpack1.h>
struct MMFS_IDENTIFICATION
{
    char *szVendor;
    char *szControllerModel;
    char *szRevision;
    char *szId;
    char *szExecutive;
    char *szKarelRevision;
    char *szProcessName;
    char *szCommRevision;
    char *szRobotModel;
}; 
#include <poppack.h>

MMFS_IDENTIFICATION mmfsId;
char** pmmfsId = (char**) &mmfsId;

for( int i = 0; i < 9; ++i )
{
    int len = buf[pos++];
    pmmfsId[i] = malloc(len+1);
    memcpy( pmmfsId[i], buf[pos], len );
}
#包括
结构MMFS_标识
{
char*szVendor;
char*szControllerModel;
char*sz修订版;
char*szId;
char*szExecutive;
char*Szkarel修订版;
char*szProcessName;
char*szcomm修订版;
char*szRobotModel;
}; 
#包括
MMFS_标识mmfsId;
字符**pmmfsId=(字符**)和mmfsId;
对于(int i=0;i<9;++i)
{
int len=buf[pos++];
pmmfsId[i]=malloc(len+1);
memcpy(pmmfsId[i],buf[pos],len);
}

由于所有结构成员都是
char*
指针,因此可以执行以下操作:

#include <pshpack1.h>
struct MMFS_IDENTIFICATION
{
    char *szVendor;
    char *szControllerModel;
    char *szRevision;
    char *szId;
    char *szExecutive;
    char *szKarelRevision;
    char *szProcessName;
    char *szCommRevision;
    char *szRobotModel;
}; 
#include <poppack.h>

MMFS_IDENTIFICATION mmfsId;
char** pmmfsId = (char**) &mmfsId;

for( int i = 0; i < 9; ++i )
{
    int len = buf[pos++];
    pmmfsId[i] = malloc(len+1);
    memcpy( pmmfsId[i], buf[pos], len );
}
#包括
结构MMFS_标识
{
char*szVendor;
char*szControllerModel;
char*sz修订版;
char*szId;
char*szExecutive;
char*Szkarel修订版;
char*szProcessName;
char*szcomm修订版;
char*szRobotModel;
}; 
#包括
MMFS_标识mmfsId;
字符**pmmfsId=(字符**)和mmfsId;
对于(int i=0;i<9;++i)
{
int len=buf[pos++];
pmmfsId[i]=malloc(len+1);
memcpy(pmmfsId[i],buf[pos],len);
}

由于您的结构包含9个具有不同名称的不同指针,因此访问它们的唯一标准方法是使用9段不同的代码。您可能试图欺骗并依赖结构的内部表示,甚至可能侥幸逃脱,但不建议这样做

使用一个函数将每个零件转换为单个衬里

void CopyString(char * & string_ptr, char * buf, int & pos)
{
    int len = buf[pos++];
    string_ptr = new char[len];
    memcpy( string_ptr, buf[pos], len );
    pos += len;
}

CopyString(mmfsId.szVendor, buf, pos);
CopyString(mmfsId.szControllerModel, buf, pos);
CopyString(mmfsId.szRevision, buf, pos);
CopyString(mmfsId.szId, buf, pos);
CopyString(mmfsId.szExecutive, buf, pos);
CopyString(mmfsId.szKarelRevision, buf, pos);
CopyString(mmfsId.szProcessName, buf, pos);
CopyString(mmfsId.szCommRevision, buf, pos);
CopyString(mmfsId.szRobotModel, buf, pos);

由于您的结构包含9个具有不同名称的不同指针,因此访问它们的唯一标准方法是使用9段不同的代码。您可能试图欺骗并依赖结构的内部表示,甚至可能侥幸逃脱,但不建议这样做

使用一个函数将每个零件转换为单个衬里

void CopyString(char * & string_ptr, char * buf, int & pos)
{
    int len = buf[pos++];
    string_ptr = new char[len];
    memcpy( string_ptr, buf[pos], len );
    pos += len;
}

CopyString(mmfsId.szVendor, buf, pos);
CopyString(mmfsId.szControllerModel, buf, pos);
CopyString(mmfsId.szRevision, buf, pos);
CopyString(mmfsId.szId, buf, pos);
CopyString(mmfsId.szExecutive, buf, pos);
CopyString(mmfsId.szKarelRevision, buf, pos);
CopyString(mmfsId.szProcessName, buf, pos);
CopyString(mmfsId.szCommRevision, buf, pos);
CopyString(mmfsId.szRobotModel, buf, pos);

目前,为了稍微简单一点的演示代码,我将成员
std::string
改为
char*
,但总体思路应该与
char*
相同

#包括
#包括
#包括
#包括
结构MMFS_标识
{
字符串供应商;
std::字符串控制器模型;
字符串修订;
std::字符串Id;
字符串执行器;
std::字符串KarelRevision;
std::字符串进程名;
std::字符串CommRevision;
字符串机器人模型;
};
int main(){
MMFS_标识;
//我们需要一个char*,因为我们需要添加字节偏移:
字符*基=(字符*)&s;
typedef std::string*ptr;
ptr字段[9];
//用字段的地址设置“fields”数组。
//请注意,parens强制在'char*`上执行指针算法,*然后*转换
//到'std::string*`。
字段[0]=(标准::字符串*)(基数+偏移量(MMF_标识,供应商));
字段[1]=(标准::字符串*)(基+偏移量(MMFS_标识,控制器模型));
字段[2]=(标准::字符串*)(基数+偏移量(MMF_标识,修订版));
字段[3]=(标准::字符串*)(基+偏移量(MMFS_标识,Id));
字段[4]=(标准::字符串*)(基+偏移量(MMF_标识,执行));
字段[5]=(标准::字符串*)(基+偏移量(MMFS_标识,KarelRevision));
字段[6]=(标准::字符串*)(基+偏移量(MMFS_标识,进程名));
字段[7]=(标准::字符串*)(基+偏移量(MMFS_标识,CommRevision));
字段[8]=(标准::字符串*)(基+偏移量(MMFS_标识,机器人模型));
//将字段内容初始化为一些可识别的值:

对于(int i=0;i目前,为了稍微简化演示代码,我将成员
std::string
改为
char*
,但总体思路应该与
char*
相同

#包括
#包括
#包括
#包括
结构MMFS_标识
{
字符串供应商;
std::字符串控制器模型;
字符串修订;
std::字符串Id;
字符串执行器;
std::字符串KarelRevision;
std::字符串进程名;
std::字符串CommRevision;
字符串机器人模型;
};
int main(){
MMFS_标识;
//我们需要一个char*,因为我们需要添加字节偏移:
字符*基=(字符*)&s;
typedef std::string*ptr;
ptr字段[9];
//用字段的地址设置“fields”数组。
//请注意,parens强制在'char*`上执行指针算法,*然后*转换
//到'std::string*`。
字段[0]=(标准::字符串*)(基数+偏移量(MMF_标识,供应商));
字段[1]=(标准::字符串*)(基+偏移量(MMFS_标识,控制器模型));
字段[2]=(标准::字符串*)(基数+偏移量(MMF_标识,修订版));
字段[3]=(标准::字符串*)(基+偏移量(MMFS_标识,Id));
字段[4]=(标准::字符串*)(基+偏移量(MMF_标识,执行));
字段[5]=(标准::字符串*)(基+偏移量(MMFS_标识,KarelRevision));
字段[6]=(标准::字符串*)(基+偏移量(MMFS_标识,进程名));
字段[7]=(标准::字符串*)(基+偏移量(MMFS_标识,CommRevision));
字段[8]=(标准::字符串*)(基+偏移量(MMFS_标识,机器人模型));
//将字段内容初始化为一些可识别的值:

(int i=0;为什么在C++中使用<代码> char *<代码>?>代码> STD::STR