这个问题在这里已有答案:
为什么我们不能使用双指针来表示二维数组? 5个答案 在C 4答案中 传递2D指针数组我有这个代码用于矩阵乘法,但它给了我一个编译错误。 我需要一个函数来接收3个矩阵的指针及其维数N作为参数。
#include <stdio.h> #define N 100 void matrixmul(int **matA, int **matB, int **matC, int n){ int i,j,k; for(i=0;i<n;i++){ for(j=0;j<n;j++){ matC[i][j] = 0; for(k=0;k<n;k++){ matC[i][j] += matA[i][k] * matB[k][j]; } } } } int main(){ int matA[N][N]={1}; int matB[N][N]={3}; int matC[N][N]; int i,j,k; matrixmul(&matA, &matB, &matC, N); for(i=0;i<N;i++){ for(j=0;j<N;j++){ printf("%d ", matC[i][j]); } printf("\n"); } return 0; }错误是:
teste.c: In function ‘main’: teste.c:28:5: warning: passing argument 1 of ‘matrixmul’ from incompatible pointer type [enabled by default] teste.c:5:6: note: expected ‘int **’ but argument is of type ‘int (*)[100][100]’ teste.c:28:5: warning: passing argument 2 of ‘matrixmul’ from incompatible pointer type [enabled by default] teste.c:5:6: note: expected ‘int **’ but argument is of type ‘int (*)[100][100]’ teste.c:28:5: warning: passing argument 3 of ‘matrixmul’ from incompatible pointer type [enabled by default] teste.c:5:6: note: expected ‘int **’ but argument is of type ‘int (*)[100][100]’This question already has an answer here:
Why can't we use double pointer to represent two dimensional arrays? 5 answers Passing 2D Array of Pointers in C 4 answersI have this code for matrix multiplication, but its given me a compilation error. I need t have a function that receives as arguments the pointers for the 3 matrices and their dimension N.
#include <stdio.h> #define N 100 void matrixmul(int **matA, int **matB, int **matC, int n){ int i,j,k; for(i=0;i<n;i++){ for(j=0;j<n;j++){ matC[i][j] = 0; for(k=0;k<n;k++){ matC[i][j] += matA[i][k] * matB[k][j]; } } } } int main(){ int matA[N][N]={1}; int matB[N][N]={3}; int matC[N][N]; int i,j,k; matrixmul(&matA, &matB, &matC, N); for(i=0;i<N;i++){ for(j=0;j<N;j++){ printf("%d ", matC[i][j]); } printf("\n"); } return 0; }The error is:
teste.c: In function ‘main’: teste.c:28:5: warning: passing argument 1 of ‘matrixmul’ from incompatible pointer type [enabled by default] teste.c:5:6: note: expected ‘int **’ but argument is of type ‘int (*)[100][100]’ teste.c:28:5: warning: passing argument 2 of ‘matrixmul’ from incompatible pointer type [enabled by default] teste.c:5:6: note: expected ‘int **’ but argument is of type ‘int (*)[100][100]’ teste.c:28:5: warning: passing argument 3 of ‘matrixmul’ from incompatible pointer type [enabled by default] teste.c:5:6: note: expected ‘int **’ but argument is of type ‘int (*)[100][100]’最满意答案
指针不是数组 。 所有&matA , &matB和&matC都是int (*)[100][100] (指向100个100个整数数组的数组的指针),但是你的函数matrixmul期望int **类型的参数(除了N : int type)。 将您的函数定义更改为
void matrixmul(int (*matA)[N], int (*matB)[N], int (*matC)[N], int n){ ... }并从main调用它
matrixmul(matA, matB, matC, N);Pointers are not arrays. All of the &matA, &matB and &matC are of type int (*)[100][100] (pointer to an array of 100 arrays of 100 integers) but your function matrixmul is expecting parameters of type int ** (except for N: int type). Change your function definition to
void matrixmul(int (*matA)[N], int (*matB)[N], int (*matC)[N], int n){ ... }and call it from main as
matrixmul(matA, matB, matC, N);更多推荐
发布评论