[英]How to create a pointer to a buffer in a typedef struct in C?
I getting an incompatible-pointer-types error when trying to Initialize a typedef struct with a pointer to a char buffer.尝试使用指向 char 缓冲区的指针初始化 typedef 结构时出现不兼容指针类型错误。 The struct looks like this:该结构如下所示:
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;
This is what i have tried, and i also tried to initialize with all the parameters in the struct.这是我尝试过的,我还尝试使用结构中的所有参数进行初始化。
uint8_t mKey[16] = "1234567891012131";
uint8_t *mKeyPointer = mKey;
otCryptoKey *aKey = {mKeyPointer};
Can anyone figure out why i get this error?谁能弄清楚为什么我会收到此错误?
You're creating a pointer to a otCryptoKey
and attempting to initialize it with a pointer to uint8_t
.您正在创建指向otCryptoKey
的指针并尝试使用指向uint8_t
的指针对其进行初始化。 Those types are not compatible.这些类型不兼容。
What you want is to create an otCryptoKey
object and initialize the mKey
member with that pointer.您想要的是创建一个otCryptoKey
object 并使用该指针初始化mKey
成员。
otCryptoKey aKey = {mKey, sizeof mKey, 0};
That syntax initializes a struct, not a pointer to a struct.该语法初始化结构,而不是指向结构的指针。
You should instead do: otCryptoKey aKey = {mKeyPointer};
你应该改为: otCryptoKey aKey = {mKeyPointer};
Or better yet, use named fields and lose the intermediate variable: otCryptoKey aKey = {.mKey = mKey };
或者更好的是,使用命名字段并丢失中间变量: otCryptoKey aKey = {.mKey = mKey };
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.