1

I am trying to allocate memory of a double pointer declared in main function:

int main () {

    float **a, **b, **c;

    inicializarMatriz (&a, &b, &c);
}

Inside inicializarMatriz function should malloc the matrix (double pointer).

void inicializarMatriz(float ***a, float ***b, float ***c) {
    // Reservar espacio de memoria para la matriz a
    **a = (float **) malloc ( N * sizeof(float *) );
    for (int i = 0; i < N; ++i) {
        a[i] = (float *) malloc ( N * sizeof(float) );
    }
    if ( a == NULL ) {
        fprintf(stderr, "No se ha podido asignar memoria a la matriz a");
    }

    // Reservar espacio de memoria para la matriz b
    **b = (float **) malloc ( N * sizeof(float *) );
    for (int i = 0; i < N; ++i) {
        b[i] = (float *) malloc ( N * sizeof(float) );
    }
    if ( b == NULL ) {
        fprintf(stderr, "No se ha podido asignar memoria a la matriz b");
    }

    // Reservar espacio de memoria para la matriz c
    **c = (float **) malloc ( N * sizeof(float *) );
    for (int i = 0; i < N; ++i) {
        c[i] = (float *) malloc ( N * sizeof(float) );
    }
    if ( c == NULL ) {
        fprintf(stderr, "No se ha podido asignar memoria a la matriz c");
    }
}

I have this warning: [Warning] assignment from incompatible pointer type and the program crashes. Any idea about what is wrong?

JuMoGar
  • 1,740
  • 2
  • 19
  • 46
  • 2
    `**a = (float **) malloc ( N * sizeof(float *) );` should be `*a = (float **) malloc ( N * sizeof(float *) );` (same for `b` and `c`), and don't cast malloc – David Ranieri Oct 04 '18 at 08:33
  • 4
    `a` is of type `float ***`. `*a` is of type `float **`. `**a` is of type `float *`. `***a` is of type `float`. I hope you see the pattern. – Some programmer dude Oct 04 '18 at 08:34
  • then, this assigment is wrong too, right? `a[i] = (float *) malloc ( N * sizeof(float) );` . This should be: `*a[i] = (float *) malloc ( N * sizeof(float) );` ? – JuMoGar Oct 04 '18 at 08:38
  • 3
    Don't forget about [*operator precedence*](http://en.cppreference.com/w/c/language/operator_precedence). `*a[i]` is equal to `*(a[i])` not `(*a)[i]` which you need. – Some programmer dude Oct 04 '18 at 08:42
  • Thank you. This solves my problem. Please, answer my question and I will choose you. – JuMoGar Oct 04 '18 at 08:47
  • Too many stars. I would strongly advise you to use 2D arrays instead of this. See [Correctly allocating multi-dimensional arrays](https://stackoverflow.com/questions/42094465/correctly-allocating-multi-dimensional-arrays). – Lundin Oct 04 '18 at 09:38
  • This is another good example how casting the result of malloc showed up a serious coding error. – Ctx Oct 04 '18 at 10:16
  • A review of [Do I cast the result of malloc?](http://stackoverflow.com/q/605845/995714) may also be in order. – David C. Rankin Oct 04 '18 at 13:46

1 Answers1

0

As @SomeProgrammerDude suggest, the working code is next:

int main () {

    float **a, **b, **c;

    inicializarMatriz (&a, &b, &c);
}

void inicializarMatriz(float ***a1, float ***b1, float ***c1) {
    // Reservar espacio de memoria para la matriz a
    *a1 = (float **) malloc ( N * sizeof(float *) ); // *a1 = **a
    for (int i = 0; i < N; ++i) {
        (*a1)[i] = (float *) malloc ( N * sizeof(float) ); // (*a1)[i] = a[i]
    }
    if ( a == NULL ) {
        fprintf(stderr, "No se ha podido asignar memoria a la matriz a");
    }

    // Reservar espacio de memoria para la matriz b
    *b1 = (float **) malloc ( N * sizeof(float *) ); // *b1 = **b
    for (int i = 0; i < N; ++i) {
        (*b1)[i] = (float *) malloc ( N * sizeof(float) ); // (*b1)[i] = b[i]
    }
    if ( b == NULL ) {
        fprintf(stderr, "No se ha podido asignar memoria a la matriz b");
    }

    // Reservar espacio de memoria para la matriz c
    *c1 = (float **) malloc ( N * sizeof(float *) ); // *c1 = **c
    for (int i = 0; i < N; ++i) {
        (*c1)[i] = (float *) malloc ( N * sizeof(float) ); // (*c1)[i] = c[i]
    }
    if ( c == NULL ) {
        fprintf(stderr, "No se ha podido asignar memoria a la matriz c");
    }
}

Solution of the problem is commeted inside the same line at the end.

JuMoGar
  • 1,740
  • 2
  • 19
  • 46