3

Question is make an array of 10 integers that's fine

int array[10];

Now question is

how to make a reference to an array which I have declared above ?

I tried this

int &ra = a;

But it's giving me error... Please provide me details about this error and how to make reference of an array.

4 Answers 4

14
int (&ra)[10] = a;

Alternatively, you can use a typedef to separate this into the type for "array of 10 ints" and having a reference there-to, as in:

typedef int int10[10];
int10& my_ref = a;

The problem with your int &ra = a; is that it tells the compiler to create a reference of type int that refers to an array of 10 ints... they're just not the same thing. Consider that sizeof(int) is a tenth of the size of an array of ten ints - they occupy different amounts of memory. What you've asked for with the reference's type could be satisfied by a particular integer, as in int& ra = a[0];.

I appreciate it's a bit confusing that int* p = a; is allowed - for compatibility with the less type-safe C, pointers can be used to access single elements or arrays, despite not preserving any information about the array size. That's one reason to prefer references - they add a little safety and functionality over pointers.

For examples of increased functionality, you can take sizeof my_ref and get the number of bytes in the int array (10 * sizeof(int)), whereas sizeof p would give you the size of the pointer (sizeof(int*)) and sizeof *p == sizeof(int). And you can have code like this that "captures" the array dimension for use within a function:

template <int N>
void f(int (&x)[N])
{
    std::cout << "I know this array has " << N << " elements\n";
}
Sign up to request clarification or add additional context in comments.

5 Comments

@Toni D :It's only half of my answer... what about the error I got .. please clarify me
Later edit is fine... but when I try int &ra = a it's giving me compilation error: that non const int & cannot be assigned to rvlaue ....I am asking what's this
It's an unhelpful compiler error message. You shouldn't expect compiler error messages to always explain the error in terms you can understand. I think the compiler is trying to make your code work by creating a temporary, but then it sees you have int& not const int& and it knows you cannot bind a temporary to a non-const reference. So that is the error you get. Try changing int& to const int& and you'll see you get another error, perhaps a more helpful one (but perhaps not).
Yeah It's still giving error by changing it to const ..anyway thanx for expalnation.. going to accept your answer... :)
@Omkant: you can't bind a non-const reference to a const variable without using a const_cast<>. That would defeat the protection const is designed to provide. You can bind a const reference to a non-const variable though.
3

The reference to array will have type int (&a)[10].

int array[10];
int (&a)[10] = array;

Sometimes it might be useful to simplify things a little bit using typedef

typedef int (&ArrayRef)[10];
...
ArrayRef a = array;

3 Comments

Does that simplify anything? The typedef is as complex as the original reference declaration.
Yeah, but if you pass this array reference to functions frequently, it might be easier and more explicit to use a type alias for functions parameters. This is not so great compared to using typedef for function pointers though.
It might be even more handy to use a typedef for array (like in Tony's answer).
1

This is a reference to an array of of ints of size 10:

int (&ra)[10];

so

int (&ra)[10] = a;

3 Comments

That's a reference to an int, you wanted a reference to an array.
@Omkant well, int &ra is a reference to an int for starters. Even after many years I still find some C declaration syntax confusing. I would use std::array<int> or std::tr1::array<int> instead.
My question is in this case when I try int &ra = a it's giving me compilation error: that non const int & cannot be assigned to rvlaue ....I am asking what's this .. ?
1

You can typedef the array type (the type should not be incomplete) as follow:

#define LEN 10
typedef int (array)[LEN]; 

int main(void)
{
    array arr = {1, 2, 3, 4, 5};    //define int arr[10] = {1, 2, 3, 4, 5};
    array arr2;                     //declare int arr[10];
    array *arrptr = &arr;           // pointer to array: int (*arrptr)[10] = &arr;
    array &&arrvef;                 // declare rvalue reference to array of 10 ints
    array &ref = arr;               // define reference to arr: int (&ref)[10] = arr;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.