类型" int *"的参数与" int(*)[1000]"类型的参数不兼容

时间:2014-05-31 10:53:57

标签: c matrix

我尝试使用此代码在矩阵之间进行乘法运算,但如果我尝试在另一台运行它,则会显示错误 类型" int "的参数与" int()[1000]"的类型参数不兼容 我认为问题在于我为普通数组进行了动态分配,但我将其用作2D数组...

 #include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <time.h>
#define M 1000
#define N 1000

void randmat (int mat[M][N],int,int);
void printmat (int mat1[M][N],int,int);

void msklret(int matrixA[M][N],int matrixB[M][N],int matrixC[M][N],int,int,int);
void main ()
{
int c1,c2,r1,r2; 
 int *mat1= (int *)malloc(sizeof(int)*N*M);
int *mat2= (int *)malloc(sizeof(int)*N*M);
int *mat3= (int *)malloc(sizeof(int)*N*M);


printf("enter the Dimensional of the matrix 1:\n");
scanf("%d%d",&r1,&c1);
printf("enter the Dimensional of the matrix 2:\n");
scanf("%d%d",&r2,&c2);
flushall();
while (c1!=r2)
{

printf("worng!!please enter a new Dimensional of the matrix 1:\n");
scanf("%d%d",&c1,&r1);
printf("please enter a new Dimensional of the matrix 2\n");
scanf("%d%d",&c2,&r2);
}



printf("random matrix 1:\n");
randmat(mat1,c1,r1);
printmat(mat1,c1,r1);
printf("random matrix 2:\n");
randmat(mat2,c2,r2);
printmat(mat2,c2,r2);
printf("the multiply of both matrix 1 and 2  :\n");
msklret(mat1,mat2,mat3,r1,c2,r2);
printmat(mat3,c2,r1);
getch();
}


void randmat (int matrix[M][N],int a,int b)
{
int i , j;



for(i=0;i<b;i++)
{
    for(j=0;j<a;j++)
    {
        matrix[i][j]=rand()%90 + 1;
    }
}
}

void printmat (int matrix1[][N],int x,int y)
{
    int k,l;
for(k=0;k<y;k++)
{
    for(l=0;l<x;l++)
        printf(" %4d ",matrix1[k][l]);                                              
    printf("\n");
}
}

void msklret(int matrixA[M][N],int matrixB[M][N],int matrixC[M][N],int r1,int c2,int r2)
{

int i , j,l,MultiSum = 0;


for (i = 0; i < r1; i++)
{
    for (j = 0; j < c2; j++)
    {
        for (l = 0; l < r2; l++)
            MultiSum += matrixA[i][l] * matrixB[l][j];

        matrixC[i][j] = MultiSum;
        MultiSum = 0;
    }
}
}

1 个答案:

答案 0 :(得分:3)

变化

int *mat1= (int *)malloc(sizeof(int)*N*M);
int *mat2= (int *)malloc(sizeof(int)*N*M);
int *mat3= (int *)malloc(sizeof(int)*N*M);

int (*mat1)[N]= (int (*)[N])malloc(sizeof(int)*N*M);//No need to cast the return value of malloc
int (*mat2)[N]= (int (*)[N])malloc(sizeof(int)*N*M);
int (*mat3)[N]= (int (*)[N])malloc(sizeof(int)*N*M);