gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 07:16:47 24 4
gpt4 key购买 nike

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

 #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;
}
}
}

最佳答案

改变

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);

关于c - "int *"类型的参数与 "int (*)[1000]"类型的参数不兼容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23969202/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com