我想让一个常量双指针指向一个指向常量double的常量指针。我从头开始制作它(当然我在书上做了一些搜索,然后用谷歌搜索)并思考以下三点:
const double* cp; //pointer to a constant double
double *const cp; //constant pointer
const double *const cp; //constant pointer to a constant double
我认为下一步是编写一个常量双指针
double **const cp;// double constant pointer
然后我结合最后两个语句并写下
const double *const cp = arr[0];
double **const cp1 = arr ;
其中arr是动态分配的双维数组。 之后我试着验证我做了什么,并写下了以下语句,期望产生所有错误。
**cp1 = 1; // didn't produce error
*cp1 = arr[4]; // didn't produce error
cp1 = new double*[5]; //produce error
所以问题是我无法完成上面描述的内容,一个常量双指针指向一个指向常量double的常量指针。我该怎么做?
提前致谢。
答案 0 :(得分:5)
中只有一个
const
double **const cp1 = arr ;
// ^^^^^
所以我不确定为什么你期望其他两个作业产生错误。
如果您希望所有级别都为const
,则需要
const double *const *const cp1 = arr;
// ^ cp1 is ...
// ^ a const pointer to ...
// ^ a const pointer to ...
// ^ a const double