21 Nov
2017
21 Nov
'17
9:53 p.m.
Steven D'Aprano wrote:
In C, it might be something similar to:
const int *a = &x;
Not quite, that makes whatever a points to constant, not a itself. But you can do this: typedef int *ip; int x, y; const ip p = &x; void f(void) { *p = 42; /* ok */ p = &y; /* not ok */ } Compiling this gives: % gcc -c constptr.c constptr.c: In function āfā: constptr.c:7: error: assignment of read-only variable āpā BTW, this shows that gcc thinks of const-declared things as "read-only variables" rather than constants. Which is a bit of a silly term when you think about it! -- Greg