3

I just want to know why, when I pass arrays to functions, say, for example, to initialize them, they actually initialize. Since the variables in the inic function are completely independent from the ones in the main function (no pointers involved), how is the inic function able to initialize the values of array v? Isn't array s inside the inic function a separate thing?

#include <stdio.h>

void inic(int s[], int n)
{
    int i;
    for(i=0; i<n; i++)
       s[i]=0;
}

main()
{
    int v[10];
    int x[20];

    inic(v, 10);
    inic(x, 20);
}

It might be clearer if I show this example as well:

#include <stdio.h>

void value(int n)
{
    n = 2;
}

main()
{
    int x=1;
    value(x);
    printf("X is %d\n", x); //The output would be 1. The function *value* doesn't affect X.
}

So why is it that it's different for arrays?

Yu Hao
  • 119,891
  • 44
  • 235
  • 294
m_botto
  • 67
  • 5
  • 1
    [This question](https://stackoverflow.com/questions/1641957/is-an-array-name-a-pointer-in-c) is a near duplicate, but is missing the simple fact that the conversion-to-pointer happens in function parameters. it is none-the-less a good summary for why array names behave like pointers. – WhozCraig May 11 '17 at 03:30
  • 1
    The essential difference is here between `s[i]=0;` and `n = 2;`. In the second case you are assigning to the variable, but in the first case you are assigning to the value pointed by the variable. – Ajay Brahmakshatriya May 11 '17 at 03:33
  • 1
    @WhozCraig: There is nothing special about function argument with respect to array-to-pointer conversion. An array expression is converted to a pointer expression *unless* it is in one of three specific contexts (unary `&`, unary `sizeof`, or a string literal in an initializer used to initialize an array object). A function argument is merely one of many contexts in which an array expression *is* converted to a pointer expression. And a separate rule: a parameter defined with an array type is really of pointer type. – Keith Thompson May 11 '17 at 03:42
  • @KeithThompson one of many, certainly, but nonetheless only one the OP was asking about. If it sounded like I was asserting that was the *only* context in which it happens, i wasn't. – WhozCraig May 11 '17 at 03:53
  • Exact duplicate of http://stackoverflow.com/questions/19646512/passing-an-array-by-reference-in-c – Karthick May 11 '17 at 10:05
  • the second posted example will not work. The assignment to 'n' just modifies the value on the stack that was passed to the sub function and not the value back in the callers' data. – user3629249 May 11 '17 at 16:03
  • this parameter: `int s[]` is actually a pointer to the callers' data, so changing anything based on that pointer will change the callers' data. – user3629249 May 11 '17 at 16:05

1 Answers1

6

When array is passed to function, actually pointer is passed, so whatever values you assign to array elements in called function is retained in caller function.

You can confirm this by printing address of your arrays(using %p in printf) in main and in inic, you will see they are same

Pras
  • 4,047
  • 10
  • 20