二维数组中的指针[重复](Pointers in bidimensional array [duplicate])

编程入门 行业动态 更新时间:2024-10-20 08:29:14
二维数组中的指针[重复](Pointers in bidimensional array [duplicate])

这个问题在这里已有答案:

为什么我们不能使用双指针来表示二维数组? 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 answers

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

更多推荐

本文发布于:2023-08-04 09:46:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1415283.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:指针   组中   Pointers   duplicate   array

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!