我应该如何调用此函数?
示例调用
typedef struct {
uint8_t addr_id :1;
uint8_t addr_type :7;
uint8_t addr[6];
} address_t;
// 1st const, 2nd const
// v---v v---v
int some_function(address_t const * const * my_addrs, uint8_t length) {
(void) my_addrs;
(void) length;
return 0;
}
int foo() {
const address_t addr1 = { .addr_id = 1, .addr_type = 3, .addr = { 1,2,3,4,5,6 } };
const address_t addr2 = { .addr_id = 1, .addr_type = 3, .addr = { 1,2,3,4,5,6 } };
const address_t addr3 = { .addr_id = 1, .addr_type = 3, .addr = { 1,2,3,4,5,6 } };
注意类型更改
my_whitelist[]
. 这需要一个指针数组。那些指针需要指向
const
数据由于
1st const
在上面
// address_t my_whitelist[6];
const address_t *my_whitelist[6];
my_whitelist[0] = &addr1;
my_whitelist[1] = &addr2;
my_whitelist[2] = &addr3;
my_whitelist[3] = &addr1;
my_whitelist[4] = &addr2;
my_whitelist[5] = &addr1;
uint8_t len = sizeof my_whitelist / sizeof my_whitelist[0];
注意
my\u白名单[]
做
不
必须是
常量
由于
2nd const
如上所述
const address_t * const my_whitelist[6];
. 这
第二常数
上面通知调用代码
some_function()
不会修改的数组元素
my\u白名单[]
.
return some_function(my_whitelist, len);
}
注:如果
my\u白名单[]
是一个
常量
数组,其值不能为
分配
但可能是
已初始化
.
// Example usage with a `const my_whitelist[]`
const address_t * const my_whitelist[] = { &addr1, &addr2, &addr3 };
注:
address_t const *
就像
const address_t *
. 领先于
常量
与C规范的样式匹配。
address_t const * const * my_addrs;
// same as
const address_t * const * my_addrs; // More common