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.