c - 二维数组的和

标签 c arrays sum

我尝试编写一个对二维数组求和的程序。这些值预加载在整个常驻者安排中

#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/

相关文章:

javascript - 在javascript中的字典(关联数组)中使用变量

javascript - 如何优化我的算法以使用子字符串进行搜索?

c - 对作为参数传递的数组进行打乱的函数

javascript - 如何使用lodash过滤数据

mysql - UNION ALL LEFT JOIN 和 SUM 返回不正确的值

f# 用方程式查询

mysql - 获取客户行的总和

c - 这段代码有错误吗?如果有的话,在哪里

c - 为什么 rand() 产生相差 3 或 4 的整数?

c - 无法写入堆栈(堆栈溢出)