【发布时间】:2016-07-01 20:44:41
【问题描述】:
我尝试制作一个对二维数组求和的程序。这些值在整个常量安排中预加载
#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;
}
【问题讨论】:
-
尝试从
m3声明中删除const限定符? -
@Olaf:考虑到这甚至不会编译,这如何调用未定义的行为?
-
我认为问题在于代码本身:
//Why is an assignment of read-only location?。答案,好吧,我会引用@jacob,'尝试从你的 m3 声明中删除 const 限定符?' -
@Fang:它不能编译的事实是因为你有一个很好的编译器让你无法编译它。这并没有改变它是 UB 的事实。
-
@Olaf:谢谢。我自己正在学习 C,你的评论对我来说不是很清楚。