Array problem



  • Hallo,
    ich folgenden Code:

    #include <stdio.h>
    #include <stdlib.h>
    
    void output_arr(int** array, int nrows, int ncols) {
      int i, j;
      for(i = 0; i < nrows; i++) {
        for(j = 0; j < ncols; j++) {
          printf("%d; ", array[i][j]);
        }
        printf("\n");
      }
      printf("\n");
    }
    
    int** add_arr(int** array1, int** array2, int nrows, int ncols) {
      int i, j;
      int** res;
      allocate2D(res, nrows, ncols);
      for(i = 0; i < nrows; i++) {
        for(j = 0; j < ncols; j++) {
          res[i][j] =  array1[i][j] +  array2[i][j];
        }
      }
    }
    
    void allocate2D(int** array, int nrows, int ncols) {
      /*  allocate array of pointers  */
      array = (int**)malloc(nrows*sizeof(int*));
      /*  allocate each row  */
      int i;
      for(i = 0; i < nrows; i++) {
        array[i] = (int*)malloc(ncols*sizeof(int));
      }
    }
    
    void deallocate2D(int** array, int nrows) {
      /*  deallocate each row */
      int i;
      for(i = 0; i < nrows; i++) {
        free(array[i]);
      }
      free(array);
    }
    
    int main() {
      int m1[4][4] = { 
        {0},
        {1},
        {0,1},
        {0,0,1}};
      int** m2;
      int** res;
    
      allocate2D(m2, 4, 4);
    
      m2 [][] = {
        {0},
        {1},
        {0,1},
        {0,0,1}};
    
      output_arr(m1, 4, 4);
      output_arr(m2, 4, 4);
    
      res = add_arr(m1, m2, 4, 4);
    }
    

    Und wenn ich das ganze versuche zu kompilieren bekomme ich diese Fehlermeldung:

    1$ gcc -o example example.c
    example.c:26: warning: conflicting types for 'allocate2D'
    example.c:18: warning: previous implicit declaration of 'allocate2D' was here
    example.c: In function 'main':
    example.c:56: error: syntax error before ']' token
    example.c: At top level:
    example.c:62: error: syntax error before numeric constant
    example.c:62: error: conflicting types for 'output_arr'
    example.c:4: error: previous definition of 'output_arr' was here
    example.c:62: warning: data definition has no type or storage class
    example.c:63: error: syntax error before numeric constant
    example.c:63: error: conflicting types for 'output_arr'
    example.c:4: error: previous definition of 'output_arr' was here
    example.c:63: warning: data definition has no type or storage class
    example.c:65: error: 'm1' undeclared here (not in a function)
    example.c:65: error: 'm2' undeclared here (not in a function)
    example.c:65: warning: initialization makes integer from pointer without a cast
    example.c:65: error: initializer element is not constant
    example.c:65: warning: data definition has no type or storage class
    example.c:66: error: syntax error before '}' token
    

    Was mache ich falsch?

    Viele Grüße



  • Zeilen 18/26: Wenn Du in C eine Funktion aufrufst, die zuvor nicht deklariert wurde, deklariert der Compiler sie als "int allocate2D()". Später sieht der Compiler aber die Definition mit dem Kopf "void allocate2D(int**, int, int)". Die passen nicht zusammen. Setze direkt nach den Includes eine Deklaration (Funktionskopf ohne Implementierung).

    Zeile 56: ? Was möchtest Du damit erreichen? Arrays kann man so nicht zuweisen.

    Die restlichen Fehler sind Folgefehler aus Zeile 56. Zusätzlich hat Dein Programm das Problem, dass der Zeiger auf die erste Dimension als Kopie übergeben wird. Das heisst, die zuweisung "arr = malloc..." bewirkt ausserhalb der Funktion nichts. Dafür müsstest Du einen Zeiger auf die erste Dimension übergeben:

    void allocate2D(int*** array, int nrows, int ncols) {
      /*  allocate array of pointers  */
      *array = malloc(nrows*sizeof(int*)); /* and: DO NOT CAST MALLOC :-) */
      /*  allocate each row  */
      int i;
      for(i = 0; i < nrows; i++) {
        *array[i] = malloc(ncols*sizeof(int));
      }
    }
    


  • Danke, habe den Code geändert und ich bekomme folge warninigs und Segmentation fault

    $ gcc -o example example.c
    example.c: In function 'allocate2D':
    example.c:10: warning: assignment makes integer from pointer without a cast
    example.c: In function 'main':
    example.c:61: warning: passing argument 1 of 'output_arr' from incompatible pointer type
    example.c:64: warning: passing argument 1 of 'add_arr' from incompatible pointer type
    $ ./example 
    Segmentation fault
    

    Den Code habe ich wie folgt geändert:

    #include <stdio.h>
    #include <stdlib.h>
    
    void allocate2D(int** array, int nrows, int ncols) {
      /*  allocate array of pointers  */
      *array = malloc(nrows*sizeof(int*)); /* and: DO NOT CAST MALLOC :-) */
      /*  allocate each row  */
      int i;
      for(i = 0; i < nrows; i++) {
        *array[i] = malloc(ncols*sizeof(int));
      }
    } 
    
    void output_arr(int** array, int nrows, int ncols) {
      int i, j;
      for(i = 0; i < nrows; i++) {
        for(j = 0; j < ncols; j++) {
          printf("%d; ", array[i][j]);
        }
        printf("\n");
      }
      printf("\n");
    }
    
    int** add_arr(int** array1, int** array2, int nrows, int ncols) {
      int i, j;
      int** res;
      allocate2D(res, nrows, ncols);
      for(i = 0; i < nrows; i++) {
        for(j = 0; j < ncols; j++) {
          res[i][j] =  array1[i][j] +  array2[i][j];
        }
      }
    }
    
    void deallocate2D(int** array, int nrows) {
      /*  deallocate each row */
      int i;
      for(i = 0; i < nrows; i++) {
        free(array[i]);
      }
      free(array);
    }
    
    int main() {
      int i,j;
      int m1[4][4] = { 
        {0},
        {1},
        {0,1},
        {0,0,1}};
      int** m2;
      int** res;
    
      allocate2D(m2, 4, 4);
    
      for(i = 0; i < 4; i++)
        for(j = 0; j < 4; j++)
          m2[i][j] = i+j;
    
      output_arr(m1, 4, 4);
      output_arr(m2, 4, 4);
    
      res = add_arr(m1, m2, 4, 4);
    }
    

    Was mache ich falsch?


Anmelden zum Antworten