C 使用多维数组添加两个矩阵时遇到问题
我似乎得到了第一个矩阵的输入,但当我要求用户输入第二个矩阵的输入时,程序崩溃了。为什么?没办法,我甚至试着分配内存,结果是一样的C 使用多维数组添加两个矩阵时遇到问题,c,arrays,matrix,C,Arrays,Matrix,我似乎得到了第一个矩阵的输入,但当我要求用户输入第二个矩阵的输入时,程序崩溃了。为什么?没办法,我甚至试着分配内存,结果是一样的 #include <stdlib.h> #include <stdio.h> #define MAXCOLUMNS 10 // dealing with 2D arrays, passing to function etc void read_input(int (*a)[MAXCOLUMNS], int n_rows, int n_co
#include <stdlib.h>
#include <stdio.h>
#define MAXCOLUMNS 10
// dealing with 2D arrays, passing to function etc
void read_input(int (*a)[MAXCOLUMNS], int n_rows, int n_columns);
void print_sum (int (*a)[MAXCOLUMNS], int (*b)[MAXCOLUMNS], int (*c)[MAXCOLUMNS], int n_rows, int n_columns);
int main() {
int i;
int rows;
int columns;
int (*two_d_array)[MAXCOLUMNS];
int (*two_d_array2)[MAXCOLUMNS];
int (*output)[MAXCOLUMNS];
printf("enter the number of rows\n");
scanf("%d", &rows);
printf("enter the number of columns\n");
scanf("%d", &columns);
printf("enter data into array number 1\n");
read_input(two_d_array, rows, columns);
printf("enter data for 2d array number 2\n");
read_input(two_d_array2, rows, columns);
print_sum(two_d_array, two_d_array2, output, rows, columns);
return 0;
}
void read_input(int (*a)[MAXCOLUMNS], int n_rows, int n_columns) {
int i;
int j;
for (i = 0; i < n_rows; ++i) {
for (j = 0; j < n_columns; ++j) {
printf("enter details for rows number %d and column number %d\n", i + 1, j + 1);
scanf("%d", (*(a+i)+j));
getchar();
}
}
}
void print_sum (int (*a)[MAXCOLUMNS], int (*b)[MAXCOLUMNS], int (*c)[MAXCOLUMNS], int n_rows, int n_columns) {
int i;
int j;
// computing sum
for (i = 0; i < n_rows; i++) {
for (j = 0; j < n_columns; j++) {
*(*(c+i)+j) = *(*(a+i)+j) + *(*(b+i)+j);
}
}
// printing sum
for (i = 0; i < n_rows; i++) {
printf("\n");
for (j = 0; j < n_columns; j++) {
printf("%d\t", *(*(c+i)+j));
}
}
}
#包括
#包括
#定义MAX10列
//处理2D数组、传递给函数等
无效读取输入(int(*a)[MAXCOLUMNS],int n_行,int n_列);
无效打印和(int(*a)[MAXCOLUMNS],int(*b)[MAXCOLUMNS],int(*c)[MAXCOLUMNS],int n_行,int n_列);
int main(){
int i;
int行;
int列;
int(*two_d_数组)[MAXCOLUMNS];
int(*two_d_array2)[MAXCOLUMNS];
int(*输出)[MAXCOLUMNS];
printf(“输入行数\n”);
scanf(“%d”,行和行);
printf(“输入列数\n”);
scanf(“%d”列和列);
printf(“将数据输入数组编号1\n”);
读取输入(两个数组、行、列);
printf(“为2d数组编号2输入数据”);
读取输入(两行两列);
打印和(两个数组,两个数组,输出,行,列);
返回0;
}
无效读取输入(int(*a)[MAXCOLUMNS],int n_行,int n_列){
int i;
int j;
对于(i=0;i
用于C99
#include <stdlib.h>
#include <stdio.h>
#define MAXCOLUMNS 10
void read_input(int rows, int cols, int a[rows][cols]);
void print_sum (int rows, int cols, int in1[rows][cols], int in2[rows][cols], int out[rows][cols]);
int main(void) {
int i, rows, columns;
printf("enter the number of rows\n");
scanf("%d", &rows);
printf("enter the number of columns\n");
scanf("%d", &columns);
//if(columns > MAXCOLUMNS){ fprintf(stderr, "too large!"); return 1); }
int array1[rows][columns];
int array2[rows][columns];
int array3[rows][columns];
printf("enter data into array number 1\n");
read_input(rows, columns, array1);
printf("enter data for 2d array number 2\n");
read_input(rows, columns, array2);
print_sum(rows, columns, array1, array2, array3);
return 0;
}
void read_input(int rows, int cols, int a[rows][cols]){
int i, j;
for (i = 0; i < rows; ++i) {
for (j = 0; j < cols; ++j) {
printf("enter details for rows number %d and column number %d\n", i + 1, j + 1);
scanf("%d", &a[i][j]);
}
}
}
void print_sum (int rows, int cols, int a[rows][cols], int b[rows][cols], int c[rows][cols]){
int i, j;
for (i = 0; i < rows; i++){
printf("\n");,,
for (j = 0; j < cols; j++){
c[i][j] = a[i][j] + b[i][j];
printf("%d\t", c[i][j]);
}
}
}
#包括
#包括
#定义MAX10列
无效读_输入(int行、int列、int a[rows][cols]);
无效打印和(int行,int列,int in1[行][cols],int in2[行][cols],int out[行][cols]);
内部主(空){
int i,行,列;
printf(“输入行数\n”);
scanf(“%d”,行和行);
printf(“输入列数\n”);
scanf(“%d”列和列);
//if(columns>MAXCOLUMNS){fprintf(stderr,“太大!”);返回1);}
int array1[行][列];
int array2[行][列];
int-array3[行][列];
printf(“将数据输入数组编号1\n”);
读取输入(行、列、数组1);
printf(“为2d数组编号2输入数据”);
读取输入(行、列、数组2);
打印总和(行、列、数组1、数组2、数组3);
返回0;
}
无效读_输入(int行、int列、int a[rows][cols]){
int i,j;
对于(i=0;i
对于整数**(2D_数组)
#包括
#包括
内部主(空){
int行=3;
int cols=5;
整数**数组;
int r,c;
//分配
数组=malloc(行*sizeof(int*);
对于(r=0;r
对于整数(**)[大小]
#包括
#包括
#定义最大值10
内部主(空){
int行=3;
int cols=5;
int(**数组)[MAX];
int r,c;
//分配
数组=malloc(行*sizeof(int(*)[MAX]);
对于(r=0;r
对于int(*)[大小]
#包括
#包括
#定义最大值10
内部主(空){
int行=3;
int cols=5;
int(*数组)[MAX];
int r,c;
//分配
数组=malloc(行*sizeof(int[MAX]);
//设置
对于(r=0;r
“甚至尝试分配内存”-并停止分配内存-确切原因是什么?您是否认为无论您遇到什么错误,以及不在需要内存的地方分配内存的错误,都会以某种方式抵消?我没有运行您的代码,但您至少应该初始化数组,例如,int(*two_d_array)[MAXCOLUMNS]=NULL代码>@n.m我试着像这样分配内存两个数组[i]=(int*)malloc(columns*sizeof(int));两个数组2[i]=(int*)malloc(列*sizeof(int));输出[i]=(int*)malloc(列*sizeof(int))@n、 mbut它说赋值中的不兼容类型,我哪里出错了@BLUEPIXYwhen使用**数组[MAX]?这是另一种写入(*array)[MAXSIZE]的方式,还是像*array[MAXSIZE]这样的指针数组@BluePixy我怎么知道什么时候像这样写malloc“array[i]=int*malloc(columns*sizeof(int));其中i在for循环中递增到最大行数,并且
#include <stdio.h>
#include <stdlib.h>
int main(void){
int rows = 3;
int cols = 5;
int **array;
int r, c;
//allocate
array = malloc(rows * sizeof(int*));
for(r = 0; r < rows ; ++r){
array[r] = malloc(cols * sizeof(int));
}
//set
for(r = 0; r < rows ; ++r){
for(c = 0; c < cols ; ++c){
array[r][c] = r * 10 + c;
}
}
//print
for(r = 0; r < rows ; ++r){
for(c = 0; c < cols ; ++c){
printf("%02d ", array[r][c]);
}
printf("\n");
}
}
#include <stdio.h>
#include <stdlib.h>
#define MAX 10
int main(void){
int rows = 3;
int cols = 5;
int (**array)[MAX];
int r, c;
//allocate
array = malloc(rows * sizeof(int (*)[MAX]));
for(r = 0; r < rows ; ++r){
array[r] = malloc(sizeof(int[MAX]));
}
//set
for(r = 0; r < rows ; ++r){
for(c = 0; c < cols ; ++c){
(*array[r])[c] = r * 10 + c;
}
}
//print
for(r = 0; r < rows ; ++r){
for(c = 0; c < cols ; ++c){
printf("%02d ", (*array[r])[c]);
}
printf("\n");
}
}
#include <stdio.h>
#include <stdlib.h>
#define MAX 10
int main(void){
int rows = 3;
int cols = 5;
int (*array)[MAX];
int r, c;
//allocate
array = malloc(rows * sizeof(int[MAX]));
//set
for(r = 0; r < rows ; ++r){
for(c = 0; c < cols ; ++c){
array[r][c] = r * 10 + c;
}
}
//print
for(r = 0; r < rows ; ++r){
for(c = 0; c < cols ; ++c){
printf("%02d ", array[r][c]);
}
printf("\n");
}
}