Error: assignment to expression with array type

1

I am reviewing the pointers in c, millions of years ago that I do not touch them and I need to use them, reviewing the programming book in Schaum c, chapter 10 pointers pag. 372, example 10.22 propose a sum of two matrices with notation of pointers, specifically through the concept of pointer to a set of one-dimensional arrays. And I find this problem which I can not see or understand. I hope someone can help me.

  

Sum of two tables of a number. Using pointers notation, the concept to be used will be that of a pointer variable that points to a set of arrays.

     

Each bidimensional array is processed as a pointer to a set of unidimentional integer arryas.

#include <stdio.h>
#include <stdlib.h>

#define MAXFIL 20

// Prototipo de funciones
void leerEntrada(int (*a)[MAXFIL], int nFilas, int nCols);
void calcularSuma(int (*a)[MAXFIL], int (*b)[MAXFIL], int (*c)[MAXFIL], int nFilas, int nCols);
void escribirSalida(int (*c)[MAXFIL], int nFilas, int nCols);

int main(int argc, char const *argv[])
{

    int fila, nFilas, nCols;

    // Definicón de punteros a un conjuntos de arryas unidimencionales.
    int (*a)[MAXFIL], (*b)[MAXFIL], (*c)[MAXFIL]; 

    printf("¿Cuántas filas? "); scanf("%i", &nFilas);
    printf("¿Cuántas columnas? "); scanf("%i", &nCols);

    // Reserva  inicial de memoria
    for (fila = 0; fila < nFilas; fila++)
    {
        a[fila] = (int *) malloc (nCols * sizeof(int));
        b[fila] = (int *) malloc (nCols * sizeof(int));
        c[fila] = (int *) malloc (nCols * sizeof(int));
    }

    printf("\n\nPrimera matriz:\n");
    leerEntrada(a, nFilas, nCols);
    printf("\nSegunda matriz\n");
    leerEntrada(b, nFilas, nCols);

    calcularSuma(a, b, c, nFilas, nCols);

    printf("\n\nSumas de los elementos:\n\n");
    escribirSalida(c, nFilas, nCols);

    return 0;
}

// leer una tabla de enteros
void leerEntrada(int (*a)[MAXFIL], int m, int n)
{
    int filas, col;

    for (filas = 0; filas < m; filas++)
    {
        printf("Introducir datos para la fila n° %i\n", filas + 1);
        for (col = 0; col < n; col++)
        {
            scanf("%i", (*(a + filas) + col));
        }
    }
}

// Sumar los elementos de dos matrices.
void calcularSuma(int (*a)[MAXFIL], int (*b)[MAXFIL], int (*c)[MAXFIL], int m, int n)
{
    int fila, col;

    for (fila = 0; fila < m; fila++)
    {
        for (col = 0; col < n; col++)
        {
            *(*(c + fila) + col) = *(*(a + fila) + col) + *(*(b + fila) + col);
        }
    }
}

// Salida de suma de dos matrices
void escribirSalida(int (*a)[MAXFIL], int m,int n)
{
    int fila, col;

    for (fila = 0; fila < m; fila++)
    {
        for (col = 0; col < n; col++)
        {
            printf("%i", *(*(a + fila) + col));
            printf("\n");
        }
    }
}

The error is as follows: error: assignment to expression with array type When you assign the sizes by mallaoc.

    
asked by Noe Blademar Chávez Morelos 09.01.2018 в 15:39
source

1 answer

3

First I would like to recommend that for a future occasion, reduce the example code to what fits your problem, in your case:

#include <stdio.h>
#include <stdlib.h>

#define MAXFIL 20

int main(int argc, char const *argv[])
{

    int nCols = 2;

    // Definicón de punteros a un conjuntos de arryas unidimencionales.
    int (*a)[MAXFIL];

    a[0] = (int *) malloc (nCols * sizeof(int));

    return 0;
}

It makes it easier for the rest to understand the error and now, to your answer!

This is a variable int:

int a;

This is a pointer to int:

int *a;

This is an array of pointers to int

int *a[20];

But ... how do you define a pointer to an array? Can not you?

The brackets ( [] ) are evaluated before the asterisks ( * ), so if you want the pointer to be evaluated first, you must surround it with a parenthesis, which in this case works the same as in the mathematical functions, where they tell you which operations to perform first.

This is a pointer to an array of 20 int:

int (*a)[20];

The example code that I have put to you (summary of yours), throws the following error:

  

error: incompatible types in assignment from 'int *' to 'int [20]'

Indeed, you are trying to assign a pointer to int (points to the first int reserved with malloc) to a pointer that points to an array of 20 positions.

And this is your second problem, you use malloc to reserve dynamic memory but at the same time you use variables of fixed size like MAXFIL, so it will be difficult for you to unite both worlds.

If you are booking a matrix of ints, simply reserve memory in this way:

int *a = (int *) malloc( nFilas * nCols * sizeof(int) );

You will have to change all the parameters that you have defined in the functions.

I hope it helped you understand why your compiler complains.

Greetings

    
answered by 10.01.2018 в 10:02