2

I'm getting three errors:

  1. Assignment makes integer from pointer without a cast
  2. Passing argument 1 of read from incompatible pointer type
  3. Expected int * (*)[10] but argument is of type int (*)[10][10]

Here is the code:

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

void read(int *(arr[10][10]), int row, int col) { //Third error here
    int i, j;
    for (i = 0; i < row; i++)
        for (j = 0; j < col; j++)
            scanf("%d", &arr[i][j]);
}
void multiply(int arr1[10][10], int row1, int col1,
              int arr2[10][10], int row2, int col2,
              int *prod[10][10]) { //Third error here
    int i, j, k, temp;
    for (i = 0; i < row1; i++)
        for (j = 0; j < col2; j++) {
            temp = 0;
            for (k = 0; k < col1; k++)
                temp += arr1[i][k] * arr2[k][j];
            prod[i][j] = temp; //First error here
        }
}
void display(int arr[10][10], int row, int col) {
    int i, j;
    for (i = 0; i < row; i++) {
        for (j = 0; j <col; j++)
            printf("%d\t",arr[i][j]);
        printf("\n");
    }
}
int main() {
    int a[10][10], b[10][10], c[10][10], m, n, p, q, i, j, k;
    printf("Enter the order of matrix A:");
    scanf("%d %d", &m, &n);
    printf("Enter the order of matrix B:");
    scanf("%d %d", &p, &q);
    if (n != p) {
        printf("Matrix multiplication is not possible.");
        exit(0);
    }
    printf("Enter the elements of matrix A:\n");
    read(&a, m, n); //Second error here
    printf("Enter the elements of matrix B:\n");
    read(&b, p, q); //Second error here
    multiply(a, m, n, b, p, q, &c);
    printf("Matrix A is:\n");
    display(a, m, n);
    printf("Matrix B is:\n");
    display(b, p, q);
    printf("Product matrix is:\n");
    display(c, m, q);
    return 0;
}
chqrlie
  • 131,814
  • 10
  • 121
  • 189
  • 1
    arrays are not first class objects so you shouldn't just copy what works for `int` and translate it to an array version. This code is very FOOBAR and very hard to explain thoroughly. – user3528438 Feb 03 '16 at 16:24
  • 1
    Check for duplicates before asking a question http://stackoverflow.com/questions/14548753/passing-a-multidimensional-variable-length-array-to-a-function – tano Feb 03 '16 at 17:01
  • @user3528438: I'm not sure what you mean by *FOOBAR*, maybe FUBAR? But this would be vastly overstated IMHO. The types are wrong and can easily be fixed, the rest is pretty straightforward. – chqrlie Feb 03 '16 at 17:34

1 Answers1

1

The type int *(arr[10][10]) for a function argument is not the same as int arr[10][10]. Change the prototype this way:

void read_matrix(int arr[10][10], int row, int col)

You should not name your function read as it is the name of the Posix system call used by the C library to implement the FILE* stream interface. Rename it read_matrix and invoke it this way:

printf("Enter the elements of matrix A:\n");
read_matrix(a,m,n); //Second error here
printf("Enter the elements of matrix B:\n");
read_matrix(b,p,q); //Second error here

The prototype for the matrix multiplication should be changed similarly to:

void multiply(int arr1[10][10], int row1, int col1,
              int arr2[10][10], int row2, int col2,
              int prod[10][10])

The confusion stems from the way arrays are passed to functions in C: they are passed as a pointer to their first element. We say that arrays automatically decay as pointers when used in expressions and passed as function arguments or return values. multiply should be called this way:

multiply(a, m, n, b, p, q, c);

Note that you should use braces { and } for all your for loops whose body has more than one line. Your omitting them for the outer loops in read and multiply are not strictly incorrect, but error prone and not recommended.

You should also check the return value from scanf() and validate the matrix dimensions.

Here is an improved version:

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

int matrix_read(int arr[10][10], int row, int col) {
    int i, j;

    for (i = 0; i < row; i++) {
        for (j = 0; j < col; j++) {
            if (scanf("%d", &arr[i][j]) != 1)
                return -1;
        }
    }
    return 0;
}

int matrix_multiply(int arr1[10][10], int row1, int col1,
                    int arr2[10][10], int row2, int col2,
                    int prod[10][10]) {
    int i, j, k, temp;

    if (col1 != row2)
        return -1;

    for (i = 0; i < row1; i++) {
        for (j = 0; j < col2; j++) {
            temp = 0;
            for (k = 0; k < col1; k++) {
                temp += arr1[i][k] * arr2[k][j];
            }
            prod[i][j] = temp;
        }
    }
    return 0;
}

void matrix_display(int arr[10][10], int row, int col) {
    int i, j;

    for (i = 0; i < row; i++) {
        for (j = 0; j <col; j++) {
            printf("%d\t", arr[i][j]);
        }
        printf("\n");
    }
}

int main(void) {
    int a[10][10], b[10][10], c[10][10], m = 0, n = 0, p = 0, q = 0;

    printf("Enter the order of matrix A: ");
    if (scanf("%d %d", &m, &n) != 2 || m < 1 || m > 10 || n < 1 || n > 10) {
        printf("Invalid matrix size: %d x %d.\n", m, n);
        exit(1);
    }
    printf("Enter the order of matrix B: ");
    if (scanf("%d %d", &p, &q) != 2 || p < 1 || p > 10 || q < 1 || q > 10) {
        printf("Invalid matrix size: %d x %d.\n", p, q);
        exit(1);
    }
    if (n != p) {
        printf("Matrix multiplication is not possible.");
        exit(1);
    }
    printf("Enter the elements of matrix A:\n");
    if (matrix_read(a, m, n)) {
        printf("Invalid matrix data.\n");
        exit(1);
    }
    printf("Enter the elements of matrix B:\n");
    if (matrix_read(b, p, q)) {
        printf("Invalid matrix data.\n");
        exit(1);
    }
    if (matrix_multiply(a, m, n, b, p, q, c)) {
        printf("Matrix multiplication error.\n");
        exit(1);
    }
    printf("Matrix A is:\n");
    matrix_display(a, m, n);
    printf("Matrix B is:\n");
    matrix_display(b, p, q);
    printf("Product matrix is:\n");
    matrix_display(c, m, q);
    return 0;
}
chqrlie
  • 131,814
  • 10
  • 121
  • 189
  • Thanks a lot! But can you tell me how to make changes to the array I'm passing from main() without using a pointer parameter? – Subramanian Sridharan Feb 05 '16 at 12:40
  • @SubramanianSridharan: there is no simple way to avoid passing a pointer parameter, nor is it useful. Defining the function argument as `int arr[10][10]` actually tells the compiler that the function receives a pointer to an array of arrays of 10 ints. It is equivalent to `int arr[][10]`. Passing an array of conforming size to the function passes a pointer to its first element. I posted a complete version of the code, check it out. – chqrlie Feb 05 '16 at 18:25
  • Oh thanks! I thought passing the array is similar to passing an integer wherein the changes made to the formal parameters won't reflect in the actual parameters. – Subramanian Sridharan Feb 06 '16 at 18:21