你的同事是错误的,它是C和C++一样。请尝试以下操作:
typedef struct foo_t {
int i;
} foo_t;
int main()
{
foo_t f = {123};
const foo_t *p = &f;
const foo_t **pp = &p;
printf("f.i = %d\n", (*pp)->i);
(*pp)->i = 888; // error
p->i = 999; // error
}
的Visual C++ 2008给出了最后两行以下错误:
error C2166: l-value specifies const object
error C2166: l-value specifies const object
GCC 4说:
error: assignment of read-only location '**pp'
error: assignment of read-only location '*p'
G ++ 4说:
error: assignment of data-member 'foo_t::i' in read-only structure
error: assignment of data-member 'foo_t::i' in read-only structure
来源
2008-12-03 09:45:45
csl
这也是你有时会看到替代的部分原因“的拼写建议:MYSTRUCTURE常量* * ppMyStruct;然后,你可以阅读从右到左:指向常量Mystructure的指针 – 2008-12-03 14:00:11
谁读取RTL?当然,我的客户都不::*)*我认为我们不需要RTL支持,我们是吗?* – dom0 2013-07-27 19:14:32
这是一个旧帖子,所以可能是我的comment-qustion将不会被回答:如何创建一个const指针指向const MyStructure指针? – tchronis 2015-01-31 15:22:26