数组里面的结构c ++

时间:2012-05-19 17:24:31

标签: c++ arrays struct

我将我的程序移植到C ++,但我遇到了一些麻烦。 我在C#中使用此代码创建了一个byte []:

writer.New(0x484700C0);
    {
        writer += Functions.PadString("PTEmu", 0x10);
        writer += new byte[] { 0xC9, 0x32, 0xE0, 0x4D, 0xEB, 0x01 };
        writer.SkipBytes(6);
        writer += 1;
        writer += channels.Count; // sub count


        for (int i = 0; i < channels.Count; i++)
        {
            writer += Functions.PadString(channels[i].Item4, 0x20); // len = 0x20
            writer += Functions.PadString(channels[i].Item2, 0x14);
            writer += Functions.PadString(channels[i].Item2, 0x14);
            writer += Functions.PadString(channels[i].Item2, 0x14);


            writer += channels[i].Item3; // int, size = 4
            writer += channels[i].Item3;
            writer += channels[i].Item3;
            writer += 0;
        }


        writer += Functions.PadString("Clan", 32);
        writer += Functions.PadString("127.0.0.1", 0x14);
        writer += Functions.PadString("127.0.0.1", 0x14);
        writer += Functions.PadString("127.0.0.1", 0x14);
        writer += 80;
        writer += 80;
        writer += 80;
        writer.SkipBytes(12);
    }

我想做类似的事情:

struct PT_ServerList{
    int size;
    int opcode;
    char server[0x10];
    char bla[6];
    int reserved1;
    short reserved2;
    int unknow;
    int serverCount;
    ServerInfo servers[];
};

但是在服务器[]之后我需要添加更多变量,但编译器不接受结构。我该怎么做才能解决这个问题?

2 个答案:

答案 0 :(得分:2)

使用vector

struct PT_ServerList {
    ...
    std::vector<ServerInfo> servers;
    ...
};

答案 1 :(得分:1)

你可能想要使用:

ServerInfo *servers;

(在这种情况下,您需要使用“new []”分配数组,并使用“delete []”释放它;);或者:

std::vector<ServerInfo> servers;

(在这种情况下,如果您之前没有使用std::vector,则应咨询std::vector documentation以获取有关如何使用它的信息。