繁体   English   中英

如何在 C 中的 typedef 结构中创建指向缓冲区的指针?

[英]How to create a pointer to a buffer in a typedef struct in C?

尝试使用指向 char 缓冲区的指针初始化 typedef 结构时出现不兼容指针类型错误。 该结构如下所示:

typedef struct otCryptoKey
{
    const uint8_t *mKey;       ///< Pointer to the buffer containing key. NULL indicates to use `mKeyRef`.
    uint16_t       mKeyLength; ///< The key length in bytes (applicable when `mKey` is not NULL).
    uint32_t       mKeyRef;    ///< The PSA key ref (requires `mKey` to be NULL).
} otCryptoKey;

这是我尝试过的,我还尝试使用结构中的所有参数进行初始化。

    uint8_t mKey[16] = "1234567891012131";
    uint8_t *mKeyPointer = mKey;

    otCryptoKey *aKey = {mKeyPointer};

谁能弄清楚为什么我会收到此错误?

您正在创建指向otCryptoKey的指针并尝试使用指向uint8_t的指针对其进行初始化。 这些类型不兼容。

您想要的是创建一个otCryptoKey object 并使用该指针初始化mKey成员。

otCryptoKey aKey = {mKey, sizeof mKey, 0};

该语法初始化结构,而不是指向结构的指针。

你应该改为: otCryptoKey aKey = {mKeyPointer};

或者更好的是,使用命名字段并丢失中间变量: otCryptoKey aKey = {.mKey = mKey };

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM