用C设置结构内的数组的大小,并在同一结构内设置另一个值 [英] Setting the size of an array inside a struct with a value of another value within the same struct, in C
问题描述
struct {
uint16 msg_length;
uint8 msg_type;
ProtocolVersion version;
uint16 cipher_spec_length;
uint16 session_id_length;
uint16 challenge_length;
V2CipherSpec cipher_specs[V2ClientHello.cipher_spec_length];
opaque session_id[V2ClientHello.session_id_length];
opaque challenge[V2ClientHello.challenge_length;
} V2ClientHello;
是否可以执行与上述类似的操作( http://tools.ietf.org/html/rfc5246 )?如果是这样,我该如何在C中进行编码?
Is it possible to do something similar to the above (http://tools.ietf.org/html/rfc5246)? If so how do I go about coding this inside C?
更具体地讲,此行在struct中:
To be more specific this line in the struct:
V2CipherSpec cipher_specs [V2ClientHello.cipher_spec_length];
V2CipherSpec cipher_specs[V2ClientHello.cipher_spec_length];
用途:
Uses:
V2ClientHello.cipher_spec_length
V2ClientHello.cipher_spec_length
在同一结构中定义,以设置数组的长度.
Which is defined in the same struct, to set the length of the array.
推荐答案
C不支持动态大小的数组.为了实现您的目标,可以使用类型为 V2CipherSpec
的指针作为结构变量,并在以后使用 V2ClientHello.cipher_spec_length
值分配内存.
C does not support dynamic-sized arrays. To achieve your goal, you can use a pointer of type V2CipherSpec
as structure variable and allocate memory at a later stage using the V2ClientHello.cipher_spec_length
value.
这篇关于用C设置结构内的数组的大小,并在同一结构内设置另一个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!