填充Char指针的结构

时间:2011-11-10 18:57:16

标签: c++

假设您有这种结构:

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;
}

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

3 个答案:

答案 0 :(得分:2)

由于您的结构包含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);

答案 1 :(得分:1)

由于所有结构成员都是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 );
}

答案 2 :(得分:0)

目前,为了更简单的演示代码,我已成为成员std::string而不是char *,但总体思路应与{{1}相同而 1

char *

另一种可能性是使用std / tr1 / boost元组。这些不会像上面那样让你使用运行时迭代,但它们会让你在编译时按编号访问字段。在某些情况下,您可以使用它来消除源代码级别的重复,即使它可能保留在目标代码中。


  1. 从技术上讲,你只能在POD结构中使用#include <stddef.h> #include <iostream> #include <string.h> #include <sstream> struct MMFS_IDENTIFICATION { std::string Vendor; std::string ControllerModel; std::string Revision; std::string Id; std::string Executive; std::string KarelRevision; std::string ProcessName; std::string CommRevision; std::string RobotModel; }; int main() { MMFS_IDENTIFICATION s; // We need a char *, because we need to add byte-wise offsets: char *base = (char *)&s; typedef std::string *ptr; ptr fields[9]; // Set up our `fields` array with the addresses of the fields. // Note the parens to force pointer arithmetic on `char *`, *then* conversion // to `std::string *`. fields[0] = (std::string *)(base + offsetof(MMFS_IDENTIFICATION, Vendor)); fields[1] = (std::string *)(base + offsetof(MMFS_IDENTIFICATION, ControllerModel)); fields[2] = (std::string *)(base + offsetof(MMFS_IDENTIFICATION, Revision)); fields[3] = (std::string *)(base + offsetof(MMFS_IDENTIFICATION, Id)); fields[4] = (std::string *)(base + offsetof(MMFS_IDENTIFICATION, Executive)); fields[5] = (std::string *)(base + offsetof(MMFS_IDENTIFICATION, KarelRevision)); fields[6] = (std::string *)(base + offsetof(MMFS_IDENTIFICATION, ProcessName)); fields[7] = (std::string *)(base + offsetof(MMFS_IDENTIFICATION, CommRevision)); fields[8] = (std::string *)(base + offsetof(MMFS_IDENTIFICATION, RobotModel)); // Initialize the field contents to some recognizable values: for (int i=0; i<9; i++) { std::ostringstream buffer; buffer << "Field " << i; *fields[i] = buffer.str(); } // print out some spot results to show we've written to the fields in the struct: std::cout << "printing by field names:\n"; std::cout << "Vendor = " << s.Vendor << "\n"; std::cout << "Id = " << s.Id << "\n"; std::cout << "CommRevision = " << s.CommRevision << "\n"; return 0; } (至少在C ++ 98/03下),所以它们根本不需要工作offsetof 。至少对于大多数典型的编译器(例如,VC ++,g ++),我还没有看到它对非POD类型失败,至少只要父结构没有自己的虚函数等。 / LI>