const指定子のついたポインタ
#include
main(){
int i;
i=0;
func(&i);
printf("%d\n", i);
}
int func( const int *i ){
int a;
a=0;
*i=23; // 参照先の値を変える。これはNG
i=&a; // これはオッケー
return 0;
}
コンパイル結果
u16@x61linux:~/cwork$ gcc const.c &&./a.out
const.c: In function ‘func’:
const.c:16: error: assignment of read-only location ‘*i’
考察
iの値を変更するのはokで、参照先の値を変更するのがダメ。
ということは、参照先の値に対してconst指定子が作用しているらしい。
じゃあ、ポインタiに対してconst指定するのはどうやるんだろう。