[英]Passing dynamic 3D matrix to function
我有一些可以像這樣訪問的3D矩陣數據: data[x][y][z]
。
每個維的大小僅在運行時已知。
我通過使用(byte (*)[Y][Z]) malloc(…)
轉換malloc來獲取此數據。
當我調用此函數時,我知道矩陣的尺寸,但是在編譯時卻不知道。 因此,我不知道如何在函數中聲明接收這樣的矩陣……
函數聲明中此數據的參數類型是什么?
function(byte (*matrix)[][]) // doesn't work
使用以下方法聲明功能:
function(size_t Y, size_t Z, byte matrix[][Y][Z])…
並調用:
function(Y, Z, matrix);
C支持可變長度數組(根據版本,它是可選的或必需的,但是在現代編譯器中很常見),但是被調用的函數不會自動知道尺寸。 您必須以某種方式將它們傳遞給函數,並且在其聲明中,函數必須聲明尺寸。
注意,維度通常可以是表達式; 它們不必是傳遞的確切參數。 例如,您可以這樣做:
function(size_t Y, byte matrix[][2*Y][3*Y])…
聲明一個函數,例如,當Y
為10時,該函數采用matrix[something][20][30]
。
這是一些代碼,用於說明3D數組的傳遞。 3D數組是一個正方形矩陣的數組,將它們相乘以生成最終結果。 該代碼可能不是最有效的,但是它確實說明了傳遞3D VLA值並不難。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static void mat_copy(int x, int y, const int src[x][y], int dst[x][y]);
static void mat_list_multiply(int x, int y, const int matlist[*][*][*], int result[*][*]);
static void mat_multiply(int x, int y, int z, const int mat1[*][*], const int mat2[*][*], int result[*][*]);
static void mat_print(const char *tag, int m, int n, const int matrix[*][*]);
int main(void)
{
int matrices[][4][4] =
{
//random -n 48 -- -99 99 | commalist -n 4 -B 12 -b '{ ' -T ' },' -R -W 4
{
{ 63, -61, 36, -27, },
{ 37, -86, 44, -14, },
{ 57, 10, 74, 23, },
{ -74, -52, -87, 53, },
},
{
{ -34, 89, -71, 34, },
{ -68, -44, -89, -95, },
{ -4, -44, 2, 80, },
{ 66, -33, -19, -65, },
},
{
{ -64, 11, 54, 20, },
{ -7, 75, -7, -98, },
{ 52, 48, -96, 76, },
{ -38, -46, -85, 4, },
},
};
enum { NUM_MATRICES = sizeof(matrices) / sizeof(matrices[0]) };
int result[4][4];
mat_list_multiply(3, 4, matrices, result);
for (int i = 0; i < NUM_MATRICES; i++)
{
char name[16];
snprintf(name, sizeof(name), "%s[%d]", "matrices", i);
mat_print(name, 4, 4, matrices[i]);
}
mat_print("result", 4, 4, result);
return 0;
}
static void mat_copy(int x, int y, const int src[x][y], int dst[x][y])
{
memmove(dst, src, x * y * sizeof(src[0][0])); // sizeof(src) is not OK
}
static void mat_list_multiply(int x, int y, const int matlist[x][y][y], int result[y][y])
{
int product[y][y];
mat_copy(y, y, matlist[0], product);
for (int i = 1; i < x; i++)
{
mat_multiply(y, y, y, product, matlist[i], result);
if (i != x-1)
mat_copy(y, y, result, product);
}
}
static void mat_multiply(int x, int y, int z, const int mat1[x][y], const int mat2[y][z], int result[x][z])
{
for (int i = 0; i < x; i++)
{
for (int j = 0; j < z; j++)
{
result[i][j] = 0;
for (int k = 0; k < y; k++)
result[i][j] += mat1[i][k] * mat2[k][j];
}
}
}
static void mat_print(const char *tag, int m, int n, const int matrix[m][n])
{
printf("%s (%dx%d):\n", tag, m, n);
for (int i = 0; i < m; i++)
{
const char *pad = "";
for (int j = 0; j < n; j++)
{
printf("%s%8d", pad, matrix[i][j]);
pad = " ";
}
putchar('\n');
}
}
輸出:
matrices[0] (4x4):
63 -61 36 -27
37 -86 44 -14
57 10 74 23
-74 -52 -87 53
matrices[1] (4x4):
-34 89 -71 34
-68 -44 -89 -95
-4 -44 2 80
66 -33 -19 -65
matrices[2] (4x4):
-64 11 54 20
-7 75 -7 -98
52 48 -96 76
-38 -46 -85 4
result (4x4):
-455910 66386 -1265422 -575600
-509373 79435 -1545267 -14906
-392428 -468852 -38119 -464008
137791 727227 393114 1044774
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.