Wednesday, 14 August 2013

How to write a reference to an array of 10 integers without using pointers?

How to write a reference to an array of 10 integers without using pointers?

Sorry if this is a really dumb question, but for some reason I can't
understand how to do it.
I tried to write code that makes the most sense for me, but it's not working:
int numbers[10]{};
int& numbers_ref { numbers }; // nope
int numbers_ref& { numbers }; // nope
int& numbers_ref { &numbers }; // nope
int numbers_ref& { &numbers }; // nope
Edit: I tried using [] but I still couldn't get it working without using
auto:
int& numbers_ref[] { numbers }; // nope
(int numbers_ref[])& { numbers }; // nope
(int numbers_ref[10]) & { numbers }; // nope
int []numbers_ref& { &numbers }; // nope
The only way I can get a reference is what P0W said:
auto& numbers_ref= numbers; // yay
P.S. Thank you chris, this is exactly what I wanted, but couldn't guess
the syntax:
int (&numbers_ref)[10] = numbers; // exactly right!

No comments:

Post a Comment