, float , 2D- float.
C:
#include <stdlib.h>
float (*transpose(int m, int n, float Xy[m][n]))[5000][3000] {
float (*result)[5000][3000] = malloc(sizeof(*result));
for(int i = 0; i < m; i++) {
for(int j = 0; j < n; j++) {
(*result)[i][j] = Xy[j][i];
}
}
return result;
}
: 2D- 5000 3000, , Java-, . , , ( free()).
, , :
float (*array)[5000][3000] = transpose(m, n, Xy);
i,j (*array)[i][j].
, :
free(array);
, C99 - - Xy . gcc, -std=c99
, , , , float ** 2D- "gotchas". , float ** array[i], array; sizeof "" 2D-, .
2) , C , . , . ?
, main(), . , . , , , , main().
, : . , , . , , . , , , , , , .
UPDATE ( C99):
, , Xy , .. transpose() 2D- ( m n ).
C 2D- . C99. C89, - , , , , 1D m*n. , . C , Xy[i][j] Xy_flat[i*n+j]. , transpose() Xy Xy 1D-. Xy[i][j] Xy[i*n+j]:
/* C89 version */
#include <stdlib.h>
float (*transpose2(int m, int n, float Xy[]))[5000][3000] {
float (*result)[5000][3000] = malloc(sizeof(*result));
int i, j;
for(i = 0; i < m; i++) {
for(j = 0; j < n; j++) {
(*result)[i][j] = Xy[j*n+i];
}
}
return result;
}
- Java, C .
, Xy. :
float matrix[3][3] = {{0, 1, 2}, {3, 4, 5}, {6, 7, 8}};
float (*transposed)[5000][3000] = transpose(3, 3, &matrix[0][0]);
/* Use (*transposed)[i][j]... */
free(transposed);
(*array)[5000][3000] - , , , , float 2D- . , , , :
float **allocate_mem_m(int m, int n)
{
int i;
float **arr = malloc(n*sizeof(*arr));
for(i=0;i<n;i++)
{
arr[i]=malloc(m*sizeof(**arr));
}
return arr;
}
float **transpose(int m, int n, float Xy[]) {
int i,j;
float **result = allocate_mem_m(m,n);
for(i = 0; i < m; i++) {
for(j = 0; j < n; j++) {
result[i][j] = Xy[j*n+i];
}
}
return result;
}
allocate_mem_m(), , . , float *** . .
free_mem_m(), :
void free_mem_m(int m, float **array) {
int i;
for (i = 0; i < m; i++) {
free(array[i]);
}
free(array);
}
:
#include <stdlib.h>
float **allocate_mem_m(int m, int n)
{
int i;
float **arr = malloc(n*sizeof(*arr));
for(i=0;i<n;i++)
{
arr[i]=malloc(m*sizeof(**arr));
}
return arr;
}
void free_mem_m(int m, float **array) {
int i;
for (i = 0; i < m; i++) {
free(array[i]);
}
free(array);
}
float **transpose(int m, int n, float Xy[]) {
int i,j;
float **result = allocate_mem_m(m,n);
for(i = 0; i < m; i++) {
for(j = 0; j < n; j++) {
result[i][j] = Xy[j*n+i];
}
}
return result;
}
:
int main(void) {
float Xy[3][3] = { { 0, 1, 2 }, { 3, 4, 5 }, { 6, 7, 8 } };
float **transposed = transpose(3, 3, &Xy[0][0]);
int i, j;
for (i = 0; i < 3; i++)
for (j = 0; j < 3; j++)
printf("%f ", transposed[i][j]);
printf("\n");
free_mem_m(3, transposed);
return 0;
}
, transpose() ( n m). , m n floats.
: http://ideone.com/CyNdpn
: 2D- 1D ; ( ), . C99, , .
, , 2 N . , , : http://codinghighway.com/?p=1159 http://codinghighway.com/?p=1206