如何在C语言中将双精度数组传递给函数? [英] How to pass a double array to a function in C?
问题描述
我整天都在努力解决这个问题:
I have been trying to solve this problem the whole day:
如何将双精度数组传递给函数?
How do I pass a double array to a function?
这里是一个例子:
int matrix[5][2] = { {1,2}, {3,4}, {5,6}, {7,8}, {9,10} };
我希望将此矩阵传递给名为eval_matrix
的函数
And I wish to pass this matrix to function named eval_matrix
,
void eval_matrix(int ?) {
...
}
我不知道应该用什么代替?
I can't figure out what should be in place of ?
有人可以帮助我解决这个问题吗?
Can anyone help me with this problem?
我知道数组可以作为指针传递,但是双精度数组(或三重数组)呢?
I know that an array can be passed just as a pointer, but what about a double array (or triple array?)
感谢Boda Cydo.
Thanks, Boda Cydo.
推荐答案
要用作数组,编译器必须知道数组的内部步幅,所以要么是:
To be usable as an array the compiler has to know the inner stride of the array, so it's either:
void eval_matrix( int m[5][2] ) { ...
或:
void eval_matrix( int m[][2], size_t od ) { ... /* od is the outer dimension */
或者只是:
void eval_matrix( int* p, size_t od, size_t id ) { ... /* ditto */
在任何情况下,它都是语法糖-数组会衰减为指针.
In any case it's syntactic sugar - the array is decayed to a pointer.
在前两种情况下,您可以照常引用m[i][j]
,但在第三种情况下必须手动偏移为p[i*id + j]
.
In first two cases you can reference array elements as usual m[i][j]
, but will have to offset manually in the third case as p[i*id + j]
.
这篇关于如何在C语言中将双精度数组传递给函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!