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