问问题
236 次
2 回答
1
because if it was allowed then you could inadvertently change something that was declared const
.
here's a concrete abuse example, if the rules allowed this:
char const* s = "a literal, very const";
bar(const char** pp )
{
*pp = s;
}
foo(char** arr)
{
bar(arr);
char* unconsted_s = *arr;
unconsted_s[0] = 'X';
}
This is also a FAQ. It’s often a good idea to check the FAQ (or just google) before asking.
于 2013-03-29T13:19:00.023 回答
0
It can lead to similar scenario if its allowed.
void foo(int** arr)
{
/*Can freely modify value at location pointed by a*/
**arr = 6;
}
int main()
{
/*Pointer to constant*/
int i = 5;
const int* a =&a;
/*Not a pointer to constant*/
int** aa= &a;
foo(aa);
return 0;
}
于 2013-03-29T13:26:40.267 回答