中級 › error: assignment of read-only location の原因と直し方 › パターン5
error: assignment of read-only location の原因と直し方
const int *pのように「指す先が変更不可」なポインタを介して、*pに値を書き込もうとしたときに出るエラーです。
1 #include <stdio.h> 2 3 int main(void) { 4 int level = 3; 5 const int *lp = &level; 6 ______ = 7; ^ 7 printf("%d\n", *lp); 8 return 0; 9 }
main.c:6:9: error: assignment of read-only location '*lp'
空欄を埋めてコンパイル・実行を通す
lp自体を別のアドレスに向け直すことはできますが、指している先の値をlp経由では書き換えられません。
次の問題