c 语法将指向常量数据的常量指针传递给函数 [英] c syntax passing const pointer to const data to function
问题描述
在下面的函数声明中,第二个参数是一个指向常量数据的常量指针.
In the function declaration below the second argument is a const pointer to const data.
ti_rc_t ti_uart_write_buffer(const ti_uart_t uart, const uint8_t *const data, uint32_t len);
下面是调用函数的示例代码.为什么在 BANNER_STR 之前有一个 (uint8_t *)
.这是将指向常量数据的常量指针传递给函数的常用语法吗?还有其他有效的语法吗?
Below is example code calling the function. Why is there a (uint8_t *)
before BANNER_STR. Is this the usual syntax for passing a const pointer to a const data to a function? Are there other valid syntax?
#define BANNER_STR ("Hello, world!\n\n")
ti_uart_write_buffer(TI_UART_0, (uint8_t *)BANNER_STR, sizeof(BANNER_STR));
谢谢
推荐答案
定义 BANNER_STR
是一个字符数组作为其类型,也就是一个字符串.因此,在您的函数调用中,您需要强制转换此参数以避免编译器警告,但这是调用此函数的有效方法.
The define BANNER_STR
is an array of characters as its type, in other words a string. And therefore in your function call you need to cast this argument to avoid compiler warnings but this is a valid way to call this function.
另一种有效的语法是不使用定义指令来声明包含要写入的字符串的常量变量:
An other valid syntax would be instead of using define directive to declare a constant variable which contains the string to write:
const char banner_str[] = "Hello, world!\n\n";
ti_uart_write_buffer(TI_UART_0, (uint8_t *)banner_str, sizeof(banner_str));
这篇关于c 语法将指向常量数据的常量指针传递给函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!