Passing array to function

Say there's an array: int arr[5]
What data types are passed to a function f by f(arr, &arr)?

Please help me understand that. Thank you.
void foo (int * a, int size)
{...}
foo(arr, 5);

the name of an array is converted to a pointer in c++ for you, its the same as &arr[0] to say arr.

if you had f(arr, &arr) those are int* and int** respectively. There is NO reason to use int ** here. that is useful if it were dynamic memory and not an array, if you wanted to change the pointer, but for that, a reference is better than a double pointer which is convoluted.

Last edited on
Can I say that the 2nd argument '&arr' is pointer to array of 5 int? Thank you.
it is a little risky to do that. to some, the words "a pointer to array" may imply a single pointer to the first location, if used casually without some context. Its not wrong but its easy for someone to not see what you meant here. If its in context with the code they will get it.
jonnin wrote:
if you had f(arr, &arr) those are int* and int** respectively

I'm not sure why jonin says &arr would pass an int**.
It would pass an int (*)[5].
So passing in that way retains the size information.
Similarly you could receive it as a reference to retain the size info.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
using std::cout;

void f(int *a) {
    cout << sizeof a << '\n';   // sizeof(int*)
    cout << sizeof *a << '\n';  // sizeof(int)
}

//void g(int **) { }  // nope

void g(int (*a)[5]) {
    cout << sizeof *a << '\n'; // sizeof entire array
}

void h(int (&a)[5]) {
    cout << sizeof a << '\n'; // sizeof entire array
}

int main() {
     int a[5];
     f(a);
     g(&a);
     h(a);
}

Can I say that the 2nd argument '&arr' is pointer to array of 5 int?
that's exactly what it is, an int(*)[5], or, in English, "a pointer to array of 5 int"
yes, I messed that up, listen to Dutch.
Thanks all for your explanations.
Topic archived. No new replies allowed.