我尝试编写一个对二维数组求和的程序。这些值预加载在整个常驻者安排中
#include <stdio.h>
#include <conio.h>
/*PROGRAM EJERC102 */
const int m1[2][2]={ {3,1},{4,5} };
const int m2[2][2]={ {1,3},{4,2} };
const int m3[2][2];
int f, c;
int main(){
for (f=0; f <= 1; f++)
for (c=0; c <= 1; c++)
{
m3[f][c]=(m1[f][c] + m2[f][c]); // Why is an assignment of read-only location?
printf ("(%d,%d)",f,c);
printf ("%d\n",m3[f][c]);
}
getch();
return 0;
}
最佳答案
您正在设置一个 const int 类型的数组,因此 main 函数将其视为只读值。只需删除第三个数组声明处的 const 即可:
#include <stdio.h>
/*PROGRAM EJERC102 */
const int m1[2][2]={ {3,1},{4,5} };
const int m2[2][2]={ {1,3},{4,2} };
int m3[2][2];
int f, c;
int main(){
for (f=0; f <= 1; f++)
for (c=0; c <= 1; c++)
{
m3[f][c]=(m1[f][c] + m2[f][c]); // Why is an assignment of read-only location?
printf ("(%d,%d)",f,c);
printf ("%d\n",m3[f][c]);
}
getchar();
return 0;
}
关于c - 二维数组的和,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38153648/