Code Fix

中級error: assignment of read-only location の原因と直し方 › パターン2

error: assignment of read-only location の原因と直し方

const int *pのように「指す先が変更不可」なポインタを介して、*pに値を書き込もうとしたときに出るエラーです。

 1  #include <stdio.h>
 2  
 3  int main(void) {
 4      int score = 10;
 5      const int *ptr = &score;
 6      ______ = 20;
           ^
 7      printf("%d\n", *ptr);
 8      return 0;
 9  }
main.c:6:10: error: assignment of read-only location '*ptr'

空欄を埋めてコンパイル・実行を通す

広告
広告スロット(未設定)

error: assignment of read-only location の原因と直し方

このエラーの解説と類題をまとめて見る →

広告
広告スロット(未設定)