中級 › error: assignment of read-only location の原因と直し方 › パターン3
error: assignment of read-only location の原因と直し方
const int *pのように「指す先が変更不可」なポインタを介して、*pに値を書き込もうとしたときに出るエラーです。
1 #include <stdio.h> 2 3 int main(void) { 4 int temp = 36; 5 const int *reading = &temp; 6 ________ = 40; ^ 7 printf("%d\n", *reading); 8 return 0; 9 }
main.c:6:14: error: assignment of read-only location '*reading'
空欄を埋めてコンパイル・実行を通す
読み取り専用の指す先を書き換えようとすると、コンパイル時にエラーとして検出されます。
次の問題