我需要类似于PHP的东西:

struct MSG_HEAD
{
        unsigned char c;
        unsigned char size;
        unsigned char headcode;
};

struct GET_INFO
{
        struct MSG_HEAD h;
        unsigned char Type;
        unsigned short Port;
        char Name[50];
        unsigned short Code;
};

void Example(GET_INFO * msg)
{
    printf(msg->Name);
    printf(msg->Code);
}

最佳答案

我创建了一个通用的php Struct类,用来模拟c-structs,它可能对您有用。
此处的代码和示例:http://bran.name/dump/php-struct
示例用法:

// define a 'coordinates' struct with 3 properties
$coords = Struct::factory('degree', 'minute', 'pole');

// create 2 latitude/longitude numbers
$lat = $coords->create(35, 40, 'N');
$lng = $coords->create(139, 45, 'E');

// use the different values by name
echo $lat->degree . '° ' . $lat->minute . "' " . $lat->pole;

07-24 09:45
查看更多