通过refrence传递二维数组的函数(C程序) [英] Passing two-dimensional array to a function by refrence (C Programming)
问题描述
我学球,并得到被困了一个小时,现在,有了这个code,
I'm learning pointers, and gotten stuck for an hour now, with this code,
#include <stdio.h>
int determinant(int **mat) /* int mat[3][3] works fine.. int *mat[3] doesn't.. neither does int *mat[] */
{
int det;
int a=*(*(mat+0)+0); // printf("\n%d",a);
int b=*(*(mat+0)+1); // printf("\n%d",b);
int c=*(*(mat+0)+2); // printf("\n%d",c);
int d=*(*(mat+1)+0); // printf("\n%d",d);
int e=*(*(mat+1)+1); // printf("\n%d",e);
int f=*(*(mat+1)+2); // printf("\n%d",f);
int g=*(*(mat+2)+0); // printf("\n%d",g);
int h=*(*(mat+2)+1); // printf("\n%d",h);
int i=*(*(mat+2)+2); // printf("\n%d",i);
det = a*(e*i-h*f) - b*(d*i-g*f) + c*(d*h-e*g);
return det;
}
int main()
{
int mat[3][3];
int i,j;
printf("Enter the 3 X 3 matrix:\n\n");
for (i=0;i<3;i++)
{
for (j=0;j<3;j++)
{
scanf("%d",*(mat+i)+j);
}
}
printf("\nThe determinant of the given 3 X 3 matrix is %d",determinant(mat));
return 0;
}
我觉得没有什么是错的函数调用。也许问题是在接受争论。 IDK,是不是垫
一个指向一维数组,这将再次成为一个指向数组元素,以垫
A指针的指针?
当我在打印的地方(只是为了检查)一些文字,我发现去执行,直到在功能 INT DET
之后,并在下一步的程序崩溃。垫[3] [3]
效果很好,但我想用一些 *
那里,因为正如我所说的,我'M'学习'。
I don't think anything is wrong with the function call. Maybe the problem is while accepting the arguments. Idk, isn't mat
a pointer to an 1-dimensional array, which would again be a pointer to the array element, making mat
a pointer to a pointer?
When I print some text at places (just to check), i find that the execution goes till after int det
in the function, and the program crashes in the next step.
mat [3][3]
works well, but i wanna use some *
there, because as i said, i'm 'learning'..
请帮帮忙!
感谢:)
Please help! Thanks :)
推荐答案
二维数组不衰的指针的指针。您可以将它们衰变为指针,以便您的code应该像
2D array dont decay to pointer to pointer. You can decay them to pointers so your code should look like
int determinant(int *mat) {
int det;
int a=*((mat+0)+0); // printf("\n%d",a);
int b=*((mat+0)+1); // printf("\n%d",b);
int c=*((mat+0)+2); // printf("\n%d",c);
int d=*((mat+1*3)+0); // printf("\n%d",d);
int e=*((mat+1*3)+1); // printf("\n%d",e);
int f=*((mat+1*3)+2); // printf("\n%d",f);
int g=*((mat+2*3)+0); // printf("\n%d",g);
int h=*((mat+2*3)+1); // printf("\n%d",h);
int i=*((mat+2*3)+2); // printf("\n%d",i);
det = a*(e*i-h*f) - b*(d*i-g*f) + c*(d*h-e*g);
return det;
}
以上code是只是为了说明,显示2-D阵列如何衰减为1-D阵列。
The above code is just for illustration, showing how 2-D array decays to 1-D array.
当您尝试访问使用括号数组像 A [2] [1]
则编译器正在上演你。通过展开我的意思是,通过的sizeof(类型)的乘积(如上图所示乘以3)。所以,如果你要腐烂1-D,你必须自己做。
When you try to access the array using braces like a[2][1]
then compiler does is unfolding for you. By unfolding I mean, the multiplication by sizeof(type) (as shown above multiply by 3). So if you decaying to 1-D you have to do it yourself.
还有一点要补充,始终尺寸的大小传给谁是有踏一维数组作为2-D的功能。像
One more thing to add, always pass the size of the dimension to the function who is has to tread the 1-D array as 2-D. like
int determinant(int *mat, int cols, rows);
编辑1:
只是,如果你想保持完整数组在函数调用补充说@JensGustedt ANS也行。
Just to add that @JensGustedt ans is also ok if you want to keep the arrays intact across function calls.
这篇关于通过refrence传递二维数组的函数(C程序)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!