Matrix migration migrating from Java to C, incompatible types

I need to transfer some Java methods to C, have a Java background, but I am a complete noob in C programming

In java

float[][] traspose(float Xy[][]) {
    float result[][]=new float[5000][3000];
    for(int i = 0; i < m; i++) {
        for(int j = 0; j < n; j++) {
            result[i][j] = Xy[j][i];
        }
    }
    return result;
}

My attempt to port C

float traspose(int m, int n, float Xy[m][n]) {
    int i,j;
    float result[5000][3000];
    for(i = 0; i < m; i++) {
        for(j = 0; j < n; j++) {
            result[i][j] = Xy[j][i];
        }
    }
    return result;
}

This does not work and receives an incompatible type error.

My 2 questions

1) How do I fix my code? Googling I saw some questions about the return of a matrix in C, but it is not very clear, and in most cases it was suggested to use an approach that does not imply the use of return.

2) I saw that usually such operations are written in C without approximation of return type, for example. void methods that work with constants or code are written directly mainly. Why?

EDIT

Following the recommendations, I tried to code this

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(int argc, char **argv) {
    printf("Hello World!");
    float matrix[3][3] = {{0, 1, 2}, {3, 4, 5}, {6, 7, 8}};
    printf("Matrix created\n");
    int size=3;
    print(size, size, matrix);
    float **transposedmat = transpose(size, size, &matrix[0][0]);
    printMat(size, size, transposedmat);
    return 0;
}

, , , trasposition.

PS C ( C99)

+4
3

, 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

+6

C. result - , &result[0] ( result), , result, .

result traspose. . result , , .

malloc, result ( ), . .

, : float. malloc - .

float **array;
array = malloc(rows * sizeof(float *));
for (i = 0; i < rows; i++)
{
  array[i] = malloc(cols * sizeof(float));
}

, .

for (i = 0; i < rows; i++) 
{
  for (j = 0; j < cols; j++)
  {
    array[i][j] = 0; //VALUE
  }
}
+2

The easiest way to do this is to pass the target array (the one that contains the transposed version) as a parameter to your function. This gives the responsibility of managing memory to the caller (where it should be), gives you some type safety and is much cleaner:

#include <stdio.h>

// Re-written method
void transpose(int m, int n, float srcXy[m][n], float dstYx[n][m]) {
    int i,j;
    for(i = 0; i < m; i++) {
        for(j = 0; j < n; j++) {
            dstYx[j][i] = srcXy[i][j];
        }
    }
}

// (just for demo purposes)
void print_matrix(int m, int n, float matrix[m][n]) {
  int i,j;
  for (i=0; i<m; i++ ) {
    printf("[");
    for (j=0; j<n-1; j++ ) {
      printf(" %f, ", matrix[i][j]);
    }
    printf("%f ]\n", matrix[i][j]);
  }
}

// (test the function)
int main() {
  int m = 2;
  int n = 3;
  float original[2][3] = { {1, 2, 3}, {4, 5, 6} };  
  float transposed[3][2];

  transpose(2, 3, original, transposed);

  printf("Original matrix\n\n");
  print_matrix(2, 3, original);

  printf("Transposed matrix\n\n");
  print_matrix(3, 2, transposed);
}
+1
source

Source: https://habr.com/ru/post/1541156/


All Articles