双const声明 [英] Double const declaration

查看:139
本文介绍了双const声明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我经常看到以下函数声明:

I often see the following function declaration:

some_func(const unsigned char * const buffer)
{

}

任何想法为什么const在指针名称之前重复?

Any idea why the const is repeated before the pointer name?

谢谢。

推荐答案

第一个const表示数据指向是不变的,不能改变,第二个表示指针本身不能改变:

The first const says that the data pointed to is constant and may not be changed, the second says that the pointer itself may not be changed:

char my_char = 'z';
const char* a = &my_char;
char* const b = &my_char;
const char* const c = &my_char;

a = &other_char; //fine
*a = 'c'; //error
b = &other_char; //error
*b = 'c'; //fine
c = &other_char; //error
*c = 'c'; //error

这篇关于双const声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆