Arrays are not passed by value, so you can simply use
void func(int mat[][3])
and, if you modify the values of mat
inside func
you are actually modifying it in main
.
You can use that approach if you know a priori the size of your matrix, otherwise consider working with pointers:
#include <iostream>
void f(int **m, int r, int c) {
m[0][0]=1;
}
int main () {
int **m;
int r=10,c=10;
int i;
m = (int**)malloc(r*sizeof(int*));
for (i=0; i<r;i++)
m[i] = (int*)malloc(c*sizeof(int));
f(m,r,c);
printf("%d
",m[0][0]);
for(i=0;i<r;i++)
free(m[i]);
free(m);
return 0;
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…