gpt4 book ai didi

c++ - 如何初始化可变长度的无符号字符数组?

转载 作者:太空狗 更新时间:2023-10-29 20:27:42 27 4
gpt4 key购买 nike

我通读了这个(How to initialize a unsigned char array?),但它并没有完全回答我的问题。

我知道我可以像这样创建一个字符串数组:

const char *str[] =
{
"first",
"second",
"third",
"fourth"
};

如果我想 write() 这些我可以使用:write(fd, str[3], sizeof(str[3]));

但是如果我需要一个可变长度的无符号字符数组怎么办?我试过这个:

const unsigned char *cmd[] =
{
{0xfe, 0x58},
{0xfe, 0x51},
{0xfe, 0x7c, 0x01, 0x02, 0x00, 0x23},
{0xfe, 0x3d, 0x02, 0x0f}
};

然后我收到 gcc 编译警告,例如*“围绕标量初始值设定项”*“初始化从整数生成指针而不进行强制转换”

最佳答案

这是一种方式

const unsigned char cmd1[] = {0xfe, 0x58};
const unsigned char cmd2[] = {0xfe, 0x51};
const unsigned char cmd3[] = {0xfe, 0x7c, 0x01, 0x02, 0x00, 0x23};
const unsigned char cmd4[] = {0xfe, 0x3d, 0x02, 0x0f};

const unsigned char *cmd[] =
{
cmd1,
cmd2,
cmd3,
cmd4
};

关于c++ - 如何初始化可变长度的无符号字符数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15618676/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com