89
#include <iostream>
using namespace std;

void printarray (int arg[], int length) {
    for (int n = 0; n < length; n++) {
        cout << arg[n] << " ";
        cout << "\n";
    }
}

int main ()
{
     int firstarray[] = {5, 10, 15};
     int secondarray[] = {2, 4, 6, 8, 10};
     printarray(firstarray, 3);
     printarray(secondarray, 5);

     return 0;
}

This code works, but I want to understand how is the array being passed.

When a call is made to the printarray function from the main function, the name of the array is being passed. The name of the array refers to the address of the first element of the array. How does this equate to int arg[]?

Seth Carnegie
  • 73,875
  • 22
  • 181
  • 249
Jay K
  • 1,087
  • 1
  • 10
  • 14
  • Just to be specific, the name of the array refers to the array. It can be converted to a pointer to the first element, which is what happens in most cases. – Joseph Mansfield Jan 13 '13 at 22:54
  • 2
    I propose making knatten's answer the accepted one. – wally Nov 22 '19 at 19:25
  • See also: [Passing an array as an argument to a function in C](https://stackoverflow.com/q/6567742/4561887) and [How to pass a multidimensional array to a function in C and C++](https://stackoverflow.com/q/2828648/4561887) – Gabriel Staples Aug 28 '22 at 15:21

5 Answers5

65

The syntaxes

int[]

and

int[X] // Where X is a compile-time positive integer

are exactly the same as

int*

when in a function parameter list (I left out the optional names).

Additionally, an array name decays to a pointer to the first element when passed to a function (and not passed by reference) so both int firstarray[3] and int secondarray[5] decay to int*s.

It also happens that both an array dereference and a pointer dereference with subscript syntax (subscript syntax is x[y]) yield an lvalue to the same element when you use the same index.

These three rules combine to make the code legal and work how you expect; it just passes pointers to the function, along with the length of the arrays which you cannot know after the arrays decay to pointers.

Baum mit Augen
  • 49,044
  • 25
  • 144
  • 182
Seth Carnegie
  • 73,875
  • 22
  • 181
  • 249
  • 4
    Ok. But how do you access the array from inside the function. If you use say `printArray(int* arg)`. How do you access arg inside the function body? – Gilbert May 26 '20 at 16:32
23

I just wanna add this, when you access the position of the array like

arg[n]

is the same as

*(arg + n) than means an offset of n starting from de arg address.

so arg[0] will be *arg

HolyBlackCat
  • 78,603
  • 9
  • 131
  • 207
DGomez
  • 1,450
  • 9
  • 25
20

The question has already been answered, but I thought I'd add an answer with more precise terminology and references to the C++ standard.

Two things are going on here, array parameters being adjusted to pointer parameters, and array arguments being converted to pointer arguments. These are two quite different mechanisms, the first is an adjustment to the actual type of the parameter, whereas the other is a standard conversion which introduces a temporary pointer to the first element.

Adjustments to your function declaration:

dcl.fct#5:

After determining the type of each parameter, any parameter of type “array of T” (...) is adjusted to be “pointer to T”.

So int arg[] is adjusted to be int* arg.

Conversion of your function argument:

conv.array#1

An lvalue or rvalue of type “array of N T” or “array of unknown bound of T” can be converted to a prvalue of type “pointer to T”. The temporary materialization conversion is applied. The result is a pointer to the first element of the array.

So in printarray(firstarray, 3);, the lvalue firstarray of type "array of 3 int" is converted to a prvalue (temporary) of type "pointer to int", pointing to the first element.

knatten
  • 5,191
  • 3
  • 22
  • 31
  • How do I assign the values from the pointer to the array eg `printArray(int* arr)` to another array already defined eg `int myArray[5]` from inside the `printArray` function. How do I do `myArray = arr`? – Gilbert May 26 '20 at 16:37
11

firstarray and secondarray are converted to a pointer to int, when passed to printarray().

printarray(int arg[], ...) is equivalent to printarray(int *arg, ...)

However, this is not specific to C++. C has the same rules for passing array names to a function.

Olaf Dietsche
  • 72,253
  • 8
  • 102
  • 198
0

The simple answer is that arrays are ALWAYS passed by reference and the int arg[] simply lets the compiler know to expect an array

Bob Warner
  • 175
  • 2
  • 1