Problem with dynamic allocation of bidimentional arrays in C

I need to make a product of arrays in which the user must enter the dimensions and terms of the arrays, but the program simply stops working. Follow the Code:

#include<stdio.h>
#include<stdlib.h>
int main(int argc, char const *argv[]){

int i, j, ja, linhasA, colunasA, linhasB, colunasB; 
float **A;                                              
float **B;
float **C;


printf("Insira o numero de linhas da matriz A:\n");         /* recebe o numero de Linhas de A */
scanf("%i", &linhasA);
printf("Insira o numero de colunas da matriz A:\n");        /* recebe o numero de Colunas de A */
scanf("%i", &colunasA);

A = malloc(linhasA * sizeof(int*));                         /* Aloca A */
    for (i = 0; i < linhasA; ++i)
    {
        A[linhasA] = malloc(colunasA * sizeof(int));
    }

if(A == 0 || A == NULL){                                    /* verifica se foi possivel alocar a memoria desejada */
    printf("Erro: nao foi possível alocar memoria solicitada \n");
    exit(1);
}

printf("Insira o numero de linhas da matriz B:\n");         /* recebe o numero de Linhas de B */
scanf("%i", &linhasB);

printf("Insira o numero de colunas da matriz B:\n");        /* recebe o numero de Colunas de B */
scanf("%i", &colunasB);

B = malloc(linhasB * sizeof(int*));                         /* Aloca B */ 
    for (i = 0; i < linhasB; ++i)
    {
        B[linhasB] = malloc(colunasB * sizeof(int));
    }

if(B == 0 || B == NULL){                                    /* verifica se foi possivel alocar a memoria desejada */
    printf("Erro: nao foi possível alocar memoria solicitada \n");
    exit(1);
}

/* Depois de alocados A e B, devemos alocar a matriz que conterá o resultado 
com o numero de linhas de A e o numero de colunas de B  */

C = malloc(linhasA * sizeof(int*));
    for (i = 0; i < linhasA; ++i)
    {
        C[linhasA] = malloc(colunasB * sizeof(int));
    }

if(C == 0 || C == NULL){                                    /* verifica se foi possivel alocar a memoria desejada */
    printf("Erro: nao foi possível alocar memoria solicitada \n");
    exit(1);
}


if(colunasA == linhasB){                                    /* verifica se a multiplicação é possivel */
    printf("ok!\n");
}
else{
    printf("as matrizes nao possuem uma dimenssao utilizavel\n");
    return 0;
}

for (i = 0; i < linhasA; ++i)                               /* recebe e imprime A */
{
    for (j = 0; j < colunasA; ++j)
    {
        printf("Digite o valor de A[%d][%d]\n", i+1, j+1);
        scanf("%f", &A[i][j]);
    }
}

printf("\n\n");
for (i = 0; i < linhasA; ++i)
{
    for (j = 0; j < colunasA; ++j)
    {
        printf("%f   ", A[i][j]);
    }
    printf("\n\n");
}

printf("\n\n");

for (i = 0; i < linhasB; ++i)                               /* recebe e imprime B */
{
    for (j = 0; j < colunasB; ++j)
    {
        printf("Digite o valor de B[%d][%d]\n", i+1, j+1);
        scanf("%f", &B[i][j]);
    }
}

printf("\n\n");
for (i = 0; i < linhasB; ++i)
{
    for (j = 0; j < colunasB; ++j)
    {
        printf("%f   ", B[i][j]);
    }
    printf("\n\n");
}

printf("\n\n");                                             /* faz a multiplicação */ 

for (i = 0; i < linhasA; ++i)
{
    for (j = 0; j < colunasB; ++j)
    {
        C[i][j] = 0;
        for (ja = 0; ja < colunasA; ja++)
        {
                C[i][j] = C[i][j] + (A[i][ja] * B[ja][j]);
        }
    }
}

printf("\n\n");                                             /* imprime o resultado */

for (i = 0; i < linhasA; ++i)
{
    for (j = 0; j < colunasB; ++j)
    {
        printf("%f  ", C[i][j]);
    }
    printf("\n\n");
}

return 0;}
Author: Cadar, 2016-05-03

2 answers

You are not allocating the values in the right place. The line

A[linhasA] = malloc(colunasA * sizeof(int));

Causes all allocated values to be in position linhasA so there is no position [0..n-1] in your pointer.

You must move to

for (i = 0; i < linhasA; ++i){
    A[i] = malloc(colunasA * sizeof(int));
}

Thus all pointer positions will have an allocated space. The same goes for allocating the memory of B and C.

B[linhasB] = malloc(colunasB * sizeof(int));
/*...*/
C[linhasA] = malloc(colunasB * sizeof(int));

Should stay:

B[i] = malloc(colunasB * sizeof(int));
/*...*/
C[i] = malloc(colunasB * sizeof(int));
 0
Author: Brumazzi DB, 2016-05-04 06:36:52

There is a conceptual error here: pointers to pointers are not two-dimensional arrays.

Although they are accessed in the same way using the subscriber operator [], the way the memory was allocated is different.

An n-dimensional array will always be allocated to a single memory block while what you are doing is creating a data structure consisting of multiple memory blocks allocated.

Matrices, regardless of the amount of dimensions, can be copied with the = operator while complex data structures need specific copy algorithms.

Details: https://stackoverflow.com/questions/7586702/is-2d-array-a-double-pointer

 0
Author: Lacobus, 2017-05-23 12:37:30