1 votes

Error: assignment to expression with array type

I am reviewing the pointers in c, I have not touched them for millions of years and I need to use them, reviewing in the book of programming in c of Schaum, chapter 10 pointers page 372, example 10.22 they raise a sum of two matrices with notation of pointers, specifically by means of the concept of pointer to a set of unidimensional arrays. And I run into this problem which I can't see or understand. I hope someone can help me.

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

Each bidimensional array is processed as a pointer to a set of unidimensional arrays of integers.

#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 assigning sizes using meshoc.

0 votes

Could you please indicate the row where the compilation error occurs? Just to ensure an effective response. From your description it occurs in instructions like this one: a[fila] = (int *) malloc (nCols * sizeof(int));

0 votes

I have already corrected it. Regards.

3voto

David Isla Points 1241

First of all, I would like to recommend that for a future occasion, you 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 of us to understand the error and now, to your answer!

This is an int variable:

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't you?

The square 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 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 given you (a summary of yours), throws the following error:

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

Effectively, 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 fixed size variables like MAXFIL, so you will find it difficult to merge both worlds.

If you are reserving an array of ints, simply reserve memory in this way:

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

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

I hope it has helped you to understand why your compiler is complaining.

Greetings

HolaDevs.com

HolaDevs is an online community of programmers and software lovers.
You can check other people responses or create a new question if you don't find a solution

Powered by:

X