c - 在 C 中,使用同一结构内的另一个值设置结构内数组的大小

标签 c arrays struct

    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;

是否可以做类似上面的事情(https://www.rfc-editor.org/rfc/rfc5246)?如果是这样,我该如何在 C 中对此进行编码?

更具体地说,结构中的这一行:

V2CipherSpec cipher_specs[V2ClientHello.cipher_spec_length];


用途:
> V2ClientHello.cipher_spec_length
这是在同一个结构体中定义的,用来设置数组的长度。

最佳答案

C 不支持动态大小的数组。为实现您的目标,您可以使用 V2CipherSpec 类型的指针作为结构变量,并在稍后阶段使用 V2ClientHello.cipher_spec_length 值分配内存。

关于c - 在 C 中,使用同一结构内的另一个值设置结构内数组的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25763343/

相关文章:

c - 如何填充短字符串以用作 AES key ?

c - ld找不到c文件

c++ - 查找数组中缺失的数字

c - 添加到链表的头部

c - memcpy 到动态存储结构是否安全?

c - pthread_join 自由() : invalid pointer error

c++ - Visual C++ 中的 _32_ 和 _64_ 等价物

c - C 中数组的选择排序

python - 加载 numpy 数组时遇到问题,它们在 python 中显示 pickle 数据错误

cLISP 中的列表操作