How does passing a statically allocated array by reference work?
void foo(int (&myArray)[100])
{
}
int main()
{
int a[100];
foo(a);
}
Does (&myArray)[100]
have any meaning or its just a syntax to pass any array by reference? I don’t understand separate parenthesis followed by big brackets here. Thanks.
It’s a syntax for array references – you need to use
(&array)
to clarify to the compiler that you want a reference to an array, rather than the (invalid) array of referencesint & array[100];
.EDIT: Some clarification.
These three are different ways of declaring the same function. They’re all treated as taking an
int *
parameter, you can pass any size array to them.This only accepts arrays of 100 integers. You can safely use
sizeof
onx
This is parsed as an “array of references” – which isn’t legal.