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.
3,486 7 7 gold badges 32 32 silver badges 49 49 bronze badges asked Apr 20, 2011 at 0:05 2,741 3 3 gold badges 16 16 silver badges 3 3 bronze badges Is there any Rvalue to Lvalue relation with the function parameters? Commented Apr 20, 2011 at 0:16 possible duplicate of What is useful about a reference-to-array parameter? Commented Apr 20, 2011 at 0:25EDIT: Some clarification.
void foo(int * x); void foo(int x[100]); void foo(int x[]);
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.
void foo(int (&x)[100]);
This only accepts arrays of 100 integers. You can safely use sizeof on x
void foo(int & x[100]); // error
This is parsed as an "array of references" - which isn't legal.
answered Apr 20, 2011 at 0:07 90.7k 13 13 gold badges 202 202 silver badges 192 192 bronze badges Why can't we have an array of references e.g. int a, b, c; int arr[3] = ; ? Commented Jan 7, 2016 at 13:10 Aha, found out why. Commented Jan 8, 2016 at 13:51Could someone explain why void foo(int & x[100]); is parsed as "array of references" please? Is it because the "from-right-to-left" rule? If yes, it seems to be not consistent with how void foo(int (&x)[100]); is parsed as "a reference to a array". Thanks in advance.
Commented Feb 11, 2017 at 0:52 It's not right to left, it's inside out, and [] binds tighter than &. Commented May 6, 2017 at 3:10"This only accepts arrays of 100 integers. You can safely use sizeof on x" is a good clarification to mention.
Commented Sep 22, 2022 at 8:04It's just the required syntax:
void Func(int (&myArray)[100])
^ Pass array of 100 int by reference the parameters name is myArray ;
void Func(int* myArray)
^ Pass an array. Array decays to a pointer. Thus you lose size information.
void Func(int (*myFunc)(double))
^ Pass a function pointer. The function returns an int and takes a double . The parameter name is myFunc .
18.3k 6 6 gold badges 55 55 silver badges 83 83 bronze badges answered Apr 20, 2011 at 0:08 Loki Astari Loki Astari 263k 86 86 gold badges 338 338 silver badges 570 570 bronze badges How do we pass a variable size array as a reference ? Commented Jun 29, 2017 at 8:07 @ShivamArora You templateize the function and make the size a template parameter. Commented Jun 29, 2017 at 15:53It is a syntax. In the function arguments int (&myArray)[100] parenthesis that enclose the &myArray are necessary. if you don't use them, you will be passing an array of references and that is because the subscript operator [] has higher precedence over the & operator .
E.g. int &myArray[100] // array of references
So, by using type construction () you tell the compiler that you want a reference to an array of 100 integers.
E.g int (&myArray)[100] // reference of an array of 100 ints
5,170 8 8 gold badges 51 51 silver badges 64 64 bronze badges answered Apr 20, 2011 at 1:06 17.4k 21 21 gold badges 91 91 silver badges 146 146 bronze badges"if you don't use them, you will be passing an array of references " - which of course, cannot exist, so you'd get a compile error. It's amusing to me that the operator precedence rules insist this must happen by default anyway.
Commented Aug 30, 2016 at 15:27 Is there's any more tutorial about type construction () ? Commented Dec 1, 2016 at 19:32Thanks, I needed an explanation that included the reason about operator precedence, which gives a sense to why it should be done this way.
Commented Nov 7, 2017 at 5:43@BugShotGG, thanks so much for providing me with a head-slap "oh, that's what's causing this" moment by mentioning operator precedence. I guess this is inevitable because [] needs left to right associativity while & needs right to left so they can't be in the same level. And I guess ++/-- need to be above & so pointer incrementation works as expected. Thanks again.
Commented Sep 22, 2022 at 8:03The following creates a generic function, taking an array of any size and of any type by reference:
template void my_func(T (&arr)[S]) < // do stuff >
answered Jun 4, 2020 at 18:38
User12547645 User12547645
8,159 3 3 gold badges 44 44 silver badges 89 89 bronze badges
Commented Nov 16, 2020 at 22:15
Arrays are default passed by pointers. You can try modifying an array inside a function call for better understanding.
answered Jul 1, 2015 at 23:07 Eduardo A. Fernández Díaz Eduardo A. Fernández Díaz 1,010 13 13 silver badges 26 26 bronze badgesArrays can not be passed by value. If the function receives a pointer, an array decays to a pointer to its first element. I'm not sure what you are trying to say here.
Commented Jul 19, 2015 at 8:50Array are neither passed by value nor reference. They are passed by pointers. If arrays are passed by reference by default, you will have no problem using sizeof on them. But that is not the case. Arrays decays to pointers when passed into a function.
Commented May 27, 2016 at 16:43Arrays can be passed by reference OR by degrading to a pointer. For example, using char arr[1]; foo(char arr[]). , arr degrades to a pointer; while using char arr[1]; foo(char (&arr)[1]) , arr is passed as a reference. It's notable that the former form is often regarded as ill-formed since the dimension is lost.
Commented Feb 11, 2017 at 0:14Re, "Arrays are. passed by pointers." That description sounds unconventional at best, and it may be confusing to newbies. The name of an array variable, is a valid expression whose value is a pointer the the first member of the array. If you have some function foo(T* t) , and you have an array T a[N]; then when you write foo(a); I think it would be more correct to say that you are passing a pointer, not passing an array, and you are passing the pointer by value.
Commented Jun 15, 2020 at 15:09@user3437460 Commenting on old comment since it has a lot of votes: in C++ arrays can be passed by reference.