c - Adding /*const*/ instead of const qualifier to char * param in function prototype
问题描述
I am facing a function which takes a pointer to a char
array but it performs only read operations on the mem the char *
points to.
I could add const
to the prototype directly before the char *
parameter, but this would break the rest of the non const-correct code. I decided to add a comment /*const*/
instead to indicate that the function performs only read operations.
However, this confuses my colleagues and I wonder if adding the comments is a common approach and the criticism is unjustified.
Example:
int func(const char *readat);
versus
int func(/*const*/ char *readat);
解决方案
添加const
到函数参数不会破坏任何调用代码。允许将非常量对象分配给 const 对象。
所以这是允许的:
char c;
char *p = &c;
const char *cp = p;
但这不是:
char c;
const char *cp = &c;
char *p = cp;
推荐阅读
- python - 如何为来自 uNet 的预测图像设置阈值
- android - 无法找到包含 Intent 在片段中时的配置根
- css - 即使设置为填充可用空间,弹性框中的按钮也有很小的边距
- spring-kafka - 卡夫卡恰好一次消息测试与“消费-转换-生产”集成测试
- python - 做减法和加法的Python代码?
- spring - Spring Cloud Data Flow:使用过滤器作为处理器
- scala - dotty / scala3 将映射的元组类型取消映射到其组成类型
- java - 什么是 window.addFlags(Integer.MIN_VALUE)
- html - 尝试使用 css 解决长期存在的 Chrome 浏览器问题,即在打印时短暂显示页面方向选项然后将其隐藏
- javascript - Axios.delete 从数据库中删除数据时出错,“错误:请求中止”