这两天被struct传参给郁闷死了。今天终于解决了。
比如有一个struct如下:
struct _ns1__Add_USCORESensorDataArray
{
struct xsd__base64Binary *btSensoridArr; /* optional element of type xsd:base64Binary */
struct ns1__ArrayOfUnsignedShort *u16DeviceIDArr; /* optional element of type ns1:ArrayOfUnsignedShort */
struct ns1__ArrayOfDouble *dSensorValueArr; /* optional element of type ns1:ArrayOfDouble */
unsigned short u16Length; /* required element of type xsd:unsignedShort */
};
它的子struct分别如下:
/// Built-in type "xs:base64Binary".
struct xsd__base64Binary
{ unsigned char *__ptr;
int __size;
char *id, *type, *options; // NOTE: for DIME and MTOM XOP attachments only
};
struct ns1__ArrayOfUnsignedShort
{
/// Size of array of unsigned short* is 0..unbounded
$int __sizeunsignedShort 0;
/// Array unsigned short* of length 0..unbounded
unsigned short* unsignedShort 0;
};
struct ns1__ArrayOfDouble
{
/// Size of array of double* is 0..unbounded
$int __sizedouble_ 0;
/// Array double* of length 0..unbounded
double* double_ 0;
};
struct _ns1__Add_USCORESensorDataArray a;
然后可以看到,,,如果是给a赋值,,,
必须这样赋。
struct ns1__ArrayOfUnsignedShort b;
struct ns1__ArrayOfDouble c;
unsigned short loc_i = 200;
unsigned char port_i = 1;
double d_i= 5000;
b.unsignedShort = &loc_i;
b.__sizeunsignedShort = sizeof(loc_i);
a.u16DeviceIDArr = &b;
因为虽然申明了a, 但是a没有赋初始值,然后初始值就没有初始地址。如果直接调用a会发生段错误。
此时需要把子struct 申明,如b。
然后用b一样的类型地址赋给a的值,这样才不会有段错误。