1

I have a doubt in this code that why does the values that i give in the readMat() actually get stored in a and b??

I mean isn't this call by value and not by reference?

Oh if there is any other thing i am doing wrong please tell me. I will be thankful.

Thanx in advance.

#include<stdio.h>

struct spMat
{
    int rowNo;
    int colNo;
    int value;
}a[20],b[20],c[20],d[20],e[20];

void readMat(struct spMat x[20])
{
    printf("Enter the number of rows in the sparse matrix\n");
    scanf("%d",&x[0].rowNo);
    printf("\nEnter the number of columns in the sparse matrix\n");
    scanf("%d",&x[0].colNo);
    printf("\nEnter the number of non-zero elements in the sparse matrix\n");
    scanf("%d",&x[0].value);

    int r=x[0].rowNo;
    int c=x[0].colNo;
    int nz=x[0].value;
    int i=1;

    while(i<=nz)
    {
        printf("\nEnter the row number of element number %d\n",i);
        scanf("%d",&x[i].rowNo);
        printf("\nEnter the column number of element number %d\n",i);
        scanf("%d",&x[i].colNo);
        printf("\nEnter the value of the element number %d\n",i);
        scanf("%d",&x[i].value);
        i++;
    }
}

void printMat(struct spMat x[20])
{
    int k=1,i,j;

    for(i=0;i<x[0].rowNo;i++)
    {
        for(j=0;j<x[0].colNo;j++)
        {
            if((k<=x[0].value)&&(x[k].rowNo==i)&&(x[k].colNo==j))
            {
                printf("%d\t",x[k].value);
                k++;
            }

            else
                printf("%d\t",0);
        }

        printf("\n");
    }
}

void fastTranspose(struct spMat x[20])
{

}

void addMat(struct spMat x[20], struct spMat y[20])
{

}

void multMat(struct spMat x[20], struct spMat y[20])
{

}

void main()
{
    readMat(a);
    readMat(b);
    printMat(a);
    printMat(b);
}

3 Answers3

4

Technically, C only supports call-by-value. When arrays are passed as function arguments, they "decayed" into pointers. And when you pass pointers, you are still passing it by value, i.e, the value of the pointer, but you can modify what the pointer points.

You can think it as if call-by-reference when passing pointers, but need to understand what really happened.

Yu Hao
  • 119,891
  • 44
  • 235
  • 294
  • 1
    To further blow your mind: It only applies to arrays passed directly as function parameters. Arrays in structures (`struct A { int arr[10]; };`) are fully copied when the structure is passed by value, as expected. – Medinoc Aug 19 '13 at 14:48
2

Yes, it's call-by-reference. In C, arrays are passed to functions by passing the address of the first element, just as if you'd declared your function as:

void readMat(struct spMat *x);
trojanfoe
  • 120,358
  • 21
  • 212
  • 242
  • I was thinking of it as a structure only but it is an array of structure and since array is passed through reference only hence this is call by reference. Am i saying this right? – user2696751 Aug 19 '13 at 14:38
  • 1
    -1, I think @Yu Hao's answer is more correct. It's pass by value, but the value happens to be a reference, basically. :) – unwind Aug 19 '13 at 14:40
  • @unwind I disagree. That doesn't even make sense. – trojanfoe Aug 19 '13 at 14:41
  • One last thing Do i have to close the question somehow, now that it has been answered. – user2696751 Aug 19 '13 at 14:45
  • @user2696751 You simply accept either of these answers to finish up. – trojanfoe Aug 19 '13 at 14:46
  • It does, but it's confused because arrays in C are confused. That they decay into pointers is not a problem, the problem is that it's legal to still declare them as "array" in the function. The syntax in your answer should be made mandatory. – Medinoc Aug 19 '13 at 14:47
  • @Medinoc What makes little sense to me is to acknowledge that the address of a data structure is being passed, whether implicitly or explicitly, and then to claim this is still pass-by-value. – trojanfoe Aug 19 '13 at 14:49
  • 1
    The same happens in Java, and this is still pass-by-value. Pointers passed by value. – Medinoc Aug 19 '13 at 14:51
  • @user2696751 I strongly suspect that the other answer is probably technically correct and there is no call-by-reference in C. However I also *think* you could consider a pointer to a data type/structure as being call-by-reference, given it can actually be used to do what you want with a reference. I don't think it's black-and-white, as it's clearly split the audience here. Anyway, accept the other answer if you wish, with no complaint from me. – trojanfoe Aug 19 '13 at 15:15
0

"When arrays are passed as function arguments, they "decayed" into pointers"

small ex:

void f(int b[])
{
  b++;
  *b = 5;
}
int main()
{
  int arr[] = {1,2,3,4};
  f(arr);
  printf("%d",arr[1]);
  return 0;   
}