Samuel Pereira
Samuel Pereira

Reputation: 49

How to use a pointer (to a Matrix) as an argument in a Function in C?

I'm trying to write a code in C that sum two 4x4 matrix. But I want my function to have a pointer as my arguments. The only error I'm getting is the time I'm trying to sum up in the function. Could someone help me?

#include <stdio.h>
#include <locale.h>

int i = 0, j = 0;

void calc_soma(int* mat_A, int* mat_B, int* mat_C)
{
    for (i = 0; i < 4; i++)
    {
        for (j = 0; j < 4; j++)
        {
            mat_C[i][j] = mat_A[i][j] + mat_B[i][j];
            printf("%d", mat_C[i][j]);
        }
    }
}

int main()
{
    setlocale(LC_ALL, "Portuguese");

    int i=0, j=0;
    int mA[4][4], mB[4][4], mC[4][4];
    int *mat_A, *mat_B, *mat_C;

    for(i = 0; i < 4; i++)
    {
        for (j = 0; j < 4; j++)
        {
            printf("Type in the value for Matrix A [%d][%d]: ", i, j);
            scanf_s("%d", &mA[i][j]);
        }
    }
    i, j = 0;
    for (i = 0; i < 4; i++)
    {
        for (j = 0; j < 4; j++)
        {
            printf("Type in the value for Matrix B [%d][%d]: ", i, j);
            scanf_s("%d", &mB[i][j]);
        }
    }

    *mat_A = &mA;
    *mat_B = &mB;

    return 0;
}

Upvotes: 0

Views: 491

Answers (1)

MikeCAT
MikeCAT

Reputation: 75062

The types of pointers for the arguments are wrong. You want to pass (the pointer to the first elements of) arrays like int mA[4][4];, so they should be pointers to int[4].

void calc_soma(int (*mat_A)[4], int (*mat_B)[4], int (*mat_C)[4])
{
    /* same as original */
}

They can simply be written like this:

void calc_soma(int mat_A[][4], int mat_B[][4], int mat_C[][4])
{
    /* same as original */
}

Then the function can be called like:

calc_soma(mA, mB, mC);

The purpose of mat_A and mat_B are unclear, but if you want to get pointers to the matrice like &mA, it should be int(*)[4][4]. Note that dereferencing (like *mat_A) uninitialized pointers will invoke undefined behavior.

int main()
{
    setlocale(LC_ALL, "Portuguese");

    int i=0, j=0;
    int mA[4][4], mB[4][4], mC[4][4];
    int (*mat_A)[4][4], (*mat_B)[4][4], (*mat_C)[4][4];

    /* omit */

    mat_A = &mA;
    mat_B = &mB;

    return 0;
}

To use functions like

void calc_soma(int* mat_A, int* mat_B, int* mat_C)

you should express the matrice by 1D array to match with the format. It will be like this:

#include <stdio.h>
#include <locale.h>

#define ROWS 4
#define COLS 4

int i = 0, j = 0;

void calc_soma(int* mat_A, int* mat_B, int* mat_C)
{
    for (i = 0; i < ROWS; i++)
    {
        for (j = 0; j < COLS; j++)
        {
            mat_C[i * COLS + j] = mat_A[i * COLS + j] + mat_B[i * COLS + j];
            printf("%d", mat_C[i * COLS + j]);
        }
    }
}

int main()
{
    setlocale(LC_ALL, "Portuguese");

    int i=0, j=0;
    int mA[ROWS * COLS], mB[ROWS * COLS], mC[ROWS * COLS];

    for(i = 0; i < ROWS; i++)
    {
        for (j = 0; j < COLS; j++)
        {
            printf("Type in the value for Matrix A [%d][%d]: ", i, j);
            scanf_s("%d", &mA[i * COLS + j]);
        }
    }
    i, j = 0;
    for (i = 0; i < ROWS; i++)
    {
        for (j = 0; j < COLS; j++)
        {
            printf("Type in the value for Matrix B [%d][%d]: ", i, j);
            scanf_s("%d", &mB[i * COLS + j]);
        }
    }

    calc_soma(mA, mB, mC);

    return 0;
}

Upvotes: 3

Related Questions