0

Hey Guys,

I need your expertise. Please help me.

In the following program, 2 matrices and given values and then displayed. The program works fine but I really can't understand how it works.

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

void getmat(int a[50][50],int n)
{
    int i,j;
    for(i=0;i<n;i++)
        for(j=0;j<n;j++)
            scanf("%d",&a[i][j]);
}

void printmat(int a[50][50],int n)
{
    int i,j;
    for(i=0;i<n;i++)
    {
        for(j=0;j<n;j++)
        {
            printf("%d\t",a[i][j]);
        }
        printf("\n");
    }
}

int main()
{
    int a[50][50],b[50][50];
    int n;
    printf("\nEnter Limit : ");
    scanf("%d",&n);
    getmat(a,n);
    getmat(b,n);
    printmat(a,n);
    printf("\n");
    printmat(b,n);
    return 0;
}

I have used VOID functions that technically doesnot return any values. But this program works flawlessly!! It gets the values that I input and prints them seperately. I don't get the flow of the program. Without any return statements how the Matrices A and B got their respective values remain a mystery.

I would be so grateful if you would be so kind to clear me off this doubt.

Thanks,
Ladis

2
Contributors
3
Replies
17
Views
3 Years
Discussion Span
Last Post by deceptikon
Featured Replies
  • 1

    The trick is that arrays in C are passed by simulated reference. The syntax you're using obscures it a bit, but these three declarations are functionally identical: `void getmat(int a[50][50],int n);` `void getmat(int a[][50],int n);` `void getmat(int (*a)[50],int n);` So an array is actually passed as a pointer to the … Read More

  • 1

    > And would you please tell me how to use the return statements for returning Matrices Stop using arrays and switch to vectors (or a matrix class), that's how you do it: vector<vector<int>> func(); There are some things that array types simply aren't suited for, and this is one of … Read More

1

The trick is that arrays in C are passed by simulated reference. The syntax you're using obscures it a bit, but these three declarations are functionally identical:

void getmat(int a[50][50],int n);
void getmat(int a[][50],int n);
void getmat(int (*a)[50],int n);

So an array is actually passed as a pointer to the first element, which is how you simulate pass by reference in C. And that's why when you scanf into a[i][j], it actually modifies the original array passed in from main.

The weirdness of arrays in C makes a lot more sense when you have a good handle on pointers.

Edited by deceptikon

0

Thanks a lot deceptikon

And would you please tell me how to use the return statements for returning Matrices

For example using getmat() function as an integer function

Thanks,
Ladis

Edited by Ladis

1

And would you please tell me how to use the return statements for returning Matrices

Stop using arrays and switch to vectors (or a matrix class), that's how you do it:

vector<vector<int>> func();

There are some things that array types simply aren't suited for, and this is one of them.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.