I'm trying to pass an three dimensional array to a function like this:
我正在尝试将三维数组传递给这样的函数:
void example( double*** bar ) {
// Stuff
}
int main() {
double[3][2][3] foo;
// Initialize foo
example( foo );
return 0;
}
This causes the gcc to give me "Invalid pointer type". How am I supposed to be doing this? I could just make the entire argument a one-dimensional array and arrange my data to fit with that, but is there a more elegant solution to this?
这导致gcc给我“无效的指针类型”。我该怎么做呢?我可以让整个参数成为一维数组并安排我的数据以适应它,但是有更优雅的解决方案吗?
edit:
In addition, I can't always specify the length of each sub-array, because they may be different sizes. e.g.:
另外,我不能总是指定每个子阵列的长度,因为它们可能是不同的大小。例如。:
int* foo[] = { { 3, 2, 1 }, { 2, 1 }, { 1 } };
If it helps at all, I'm trying to batch pass inputs for Neurons in a Neural Network. Each Neuron has a different number of inputs.
如果它有帮助,我正在尝试批量传递神经网络中神经元的输入。每个神经元都有不同数量的输入。
4 个解决方案
#1
just use double*
. A multidimensional array is stored contiguously in memory so you are quite welcome to give it your own stride. This is how bitmaps are passed on OpenGL.
只需使用双*。多维数组连续存储在内存中,因此非常欢迎您自己动手。这是在OpenGL上传递位图的方式。
#2
A one-dimensional int
array decays into an int
pointer when passing it to a function. A multi-dimensional array decays into a pointer to an array of the next lowest dimension, which is
将一维int数组传递给函数时,它会衰减为int指针。多维数组衰减成指向下一个最低维度的数组的指针,即
void example(double (*bar)[2][3]);
This syntax can be a bit baffling, so you might chose the equivalent syntax:
这种语法有点令人困惑,因此您可能会选择等效的语法:
void example(double bar[][2][3]) {
// Stuff
}
int main() {
double foo[3][2][3];
example(foo);
return 0;
}
The first dimension does not have to be given, it's that part that is "decaying". (Note that the dimensions of arrays are not given on the type as in Java, but on the array name.)
第一个维度不必给出,那就是“腐朽”的部分。 (请注意,数组的维度不是像Java那样在类型上给出,而是在数组名称上。)
This syntax works for variable-length arrays (VLAs) as well, as long as you pass the dimensions before the array:
只要在数组之前传递维度,此语法也适用于可变长度数组(VLA):
void example(int x, int y, double (*bar)[x][y]) {
// Stuff
}
int main() {
double foo[3][2][3];
example(2, 3, foo);
return 0;
}
This feature requires C99 and is not compatible with C++.
此功能需要C99并且与C ++不兼容。
#3
If the array size is fixed, you can use:
如果数组大小是固定的,您可以使用:
void example(double bar[][2][3]) {
}
Otherwise, you can pass the size along with the array into the function:
否则,您可以将大小与数组一起传递给函数:
void example(size_t x, size_t y, size_t z, double bar[x][y][z]) {
}
#4
That can't be done in C the way you're thinking of. If you need a function that operates on variable-size multidimensional arrays, you'll either have to pass the sizes (all but one) explicitly to the function, or make a structure and pass that. I generally always make a structure when a 2D or 3D array is called for, even if they're of fixed size. I think it's just cleaner that way, since the structure documents itself.
这不能用你想到的方式在C中完成。如果您需要一个对可变大小多维数组进行操作的函数,您必须将大小(除了一个之外)明确地传递给函数,或者创建一个结构并传递它。我通常总是在调用2D或3D数组时制作结构,即使它们具有固定大小。我认为它只是更清洁,因为结构记录了自己。
#1
just use double*
. A multidimensional array is stored contiguously in memory so you are quite welcome to give it your own stride. This is how bitmaps are passed on OpenGL.
只需使用双*。多维数组连续存储在内存中,因此非常欢迎您自己动手。这是在OpenGL上传递位图的方式。
#2
A one-dimensional int
array decays into an int
pointer when passing it to a function. A multi-dimensional array decays into a pointer to an array of the next lowest dimension, which is
将一维int数组传递给函数时,它会衰减为int指针。多维数组衰减成指向下一个最低维度的数组的指针,即
void example(double (*bar)[2][3]);
This syntax can be a bit baffling, so you might chose the equivalent syntax:
这种语法有点令人困惑,因此您可能会选择等效的语法:
void example(double bar[][2][3]) {
// Stuff
}
int main() {
double foo[3][2][3];
example(foo);
return 0;
}
The first dimension does not have to be given, it's that part that is "decaying". (Note that the dimensions of arrays are not given on the type as in Java, but on the array name.)
第一个维度不必给出,那就是“腐朽”的部分。 (请注意,数组的维度不是像Java那样在类型上给出,而是在数组名称上。)
This syntax works for variable-length arrays (VLAs) as well, as long as you pass the dimensions before the array:
只要在数组之前传递维度,此语法也适用于可变长度数组(VLA):
void example(int x, int y, double (*bar)[x][y]) {
// Stuff
}
int main() {
double foo[3][2][3];
example(2, 3, foo);
return 0;
}
This feature requires C99 and is not compatible with C++.
此功能需要C99并且与C ++不兼容。
#3
If the array size is fixed, you can use:
如果数组大小是固定的,您可以使用:
void example(double bar[][2][3]) {
}
Otherwise, you can pass the size along with the array into the function:
否则,您可以将大小与数组一起传递给函数:
void example(size_t x, size_t y, size_t z, double bar[x][y][z]) {
}
#4
That can't be done in C the way you're thinking of. If you need a function that operates on variable-size multidimensional arrays, you'll either have to pass the sizes (all but one) explicitly to the function, or make a structure and pass that. I generally always make a structure when a 2D or 3D array is called for, even if they're of fixed size. I think it's just cleaner that way, since the structure documents itself.
这不能用你想到的方式在C中完成。如果您需要一个对可变大小多维数组进行操作的函数,您必须将大小(除了一个之外)明确地传递给函数,或者创建一个结构并传递它。我通常总是在调用2D或3D数组时制作结构,即使它们具有固定大小。我认为它只是更清洁,因为结构记录了自己。