1

When a simple variable name is used as an argument passed to a function, the function takes the value corresponding to this variable name and installs it as a new variable in a new memory location created by the function for that purpose. But what happens when the address of an array is passed to a function as an argument? Does the function create another array and move the values into it from the array in the calling program?

Aaron
  • 1,969
  • 6
  • 27
  • 47

7 Answers7

4

No, if you pass the address of something to a function, the function gets the address. Technically, it gets a copy of the address (it's all pass-by-value, we just use pointers to simulate pass-by-reference) so that, if you change that address within the function, that has no effect on return.

By dereferencing the address, you can change the thing it points to. For example:

void changeValA (int x) {
    x = 42;                  // x is 42 but a is unchanged.
}
void changeValB (int *x) {
    *x = 42;                 // *x (hence a) is now 42
}
:
int a = 31415;               // a is 31415
changeValA (a);              // a is still 31415
changeValB (&a);             // a is now 42

Passing the address of an array is pretty much the same as passing the array itself. Under those circumstances, the array decays to the address of its first element.

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
  • Fine then. I'll stop typing my answer ;) – Demian Brecht May 25 '11 at 07:27
  • @Demian Brecht Oh I see. So instead of passing the values in the array, only the *address* of the array is passed. Ahmmm the function then can use the address to access the *original* array??? – Aaron May 25 '11 at 07:30
  • 1
    @aerohn: yes, that's how you do pass-by-reference in old-style C. Hopefully in the next ISO standard, we'll get the C++ `void changeValC (int &x) { x = 42; } ... changeValC (a);` reference types and one of the most newbie-unfriendly bits of C will be retired :-) – paxdiablo May 25 '11 at 07:35
  • So you mean, once it knows the address, function communicates directly with array, let say `list[]`, in `main()`??? – Aaron May 25 '11 at 07:38
  • 1
    @aerohn, yes, it has an address for the first element in the array (and the array is of course contiguous). That means it can use indexes to get at the other elements. But it doesn't have the array size - if you want the function to know that, you have to pass it in as well: `fn (myarr, sizeof(myarr)/sizeof(*myarr))`. This decay is also why `sizeof` is useless in a function when you pass in the array - it always gives you the size of the _pointer_ rather than the size of the array. – paxdiablo May 25 '11 at 07:40
1

No.

When an array is passed to a function, it decays to a pointer to the first element of the array. This allows the function to access the array elements exactly like the original code, including the ability to modify the elements that comes with passing a pointer to a function, but loses the ability to know the compile-time size of the array with sizeof.

Basically, this:

void func(int a[]);

// ...

int i[] = { 0, 1, 2, 3, 4 };
func(i);

Is effectively doing this:

void func(int *a);

// ...

int i[] = { 0, 1, 2, 3, 4 };
func( & (i[0]) );

Note that the function receiving the array has no way of knowing how long it is because of this. That's why almost all functions that take arrays (or pointers) also take a size parameter.

Chris Lutz
  • 73,191
  • 16
  • 130
  • 183
0

The numeric value of the address will be copied to the stack, not the memory space that the address points to!

Robert
  • 878
  • 6
  • 14
0

The name of an array can be treated as a pointer to its start. So when you pass the array to a function, it's as if you were passing a pointer. No new array is created.

George Kastrinis
  • 4,924
  • 4
  • 29
  • 46
0

No. Only the reference/address of the array will go. So any update within the function on the array will affect the passed array.

itsazzad
  • 6,868
  • 7
  • 69
  • 89
0

In C, an array is always passed by reference (i.e. only the pointer to its beginning is passed).

If you really want to pass an array by value, you can wrap it in a struct, and pass the struct. There is no other way to pass an array by value.

Blagovest Buyukliev
  • 42,498
  • 14
  • 94
  • 130
-1

No when you pass an address of an array (I believe you are talking about pointers) the copy of the pointer is passed to the function.

Naveen
  • 74,600
  • 47
  • 176
  • 233