Given the following C-code:
#include <stdio.h>
int mat1[2][4] = {
{9, 10, 11, 12},
{13, 14, 15, 16}
};
int (*(transpose)(int matrix[][4]))[2] {
static int mat[4][2];
int i;
int j;
printf("I am the function transpose()\nand I'm transposing 2x4 matrices.\n\n");
for (i = 0; i < 2; i++) {
for (j = 0; j < 4; j++) {
mat[j][i] = matrix[i][j];
}
}
return mat;
}
int main() {
int (*mat_transpose)[2];
int i;
int j;
mat_transpose = transpose(mat1);
for (j = 0; j < 2; j++) {
for (i = 0; i < 4; i++) {
printf("mat_transpose[%d][%d] = %d\n", i, j, mat_transpose[i][j]);
}
}
return 0;
}
The function transpose()returns a two-dimensional array (or, rather, a pointer to an array of pointers). Is this the right way to achieve this? Looking at various issues related to Stackoverflow, it seems that there is no standard way to do this, and that is a lot. Is there any standard regarding returning two- or multi-dimensional arrays?
source
share