Click here to Skip to main content
15,885,216 members
Please Sign up or sign in to vote.
5.00/5 (2 votes)
See more:
Hi,
My code looks like this:
#include <iostream>
     using namespace std;
     int main() {
      int ix,iy,iz;
      int *ixPtr = &ix,*&ixPtrRef=ixPtr;
      //the following statement flags a compiler error
      int &intArrayOfRefs[] = {ix,iy,iz};
      return 0;
     }
></iostream>

But why is array of references is not allowed in C++
VB
error C2234: 'iArrayOfRefs' : arrays of references are illegal  
Posted
Comments
Stefan_Lang 30-May-11 5:30am    
+5 to your question.
I've been using C++ for 20+ years, but never found any good explanation for this (but see my attempt below)
hakz.code 31-May-11 1:29am    
Thank you for +5,please see my comment below.

Basically, a reference is an alias to an existing variable. This means, if you apply any operation on a reference, it will behave as if you were using the original variable name. This principle also applies to taking the address of it - and as a result, if you take the address of a reference, you will not get a pointer to the reference variable, as you might think, instead you get a pointer to the address that holds the variables value:
int original = 5;
int& alias = original; // refers to 5
int* p_original = &original; // let's assume that: p_original == 0x01231234
int* p_alias = &alias; // p_alias == 0x01231234
(int&)* pr_alias = &alias; // error! (&alias has the type int*)

If you wanted to create an array of references, the array symbol itself would be considered a pointer to an array element. Since taking the address of a reference yields a pointer to the original element, the type of the pointer will be pointer to original element type, not pointer to reference of (...). Therefore using a reference as an element type for an array is considered illegal.

( I have to admit I'm not entirely happy with that last part, but I've never come across a better explanation )
 
Share this answer
 
By the way the language is designed, a reference is an alias to something else and must be initialized on instantiation.
But the language doesn't provide any mechanism to initialize array elemets all together.

The only possible way should be a sort of language extension allowing things like
C++
int a,b,c;
int& v[] = { a, b, c };
// v[0] aliases a, v[1] does b and v[2] does c.

But that's not feasible being a b & c not constant.
 
Share this answer
 
v2
Comments
Richard MacCutchan 30-May-11 10:30am    
It's not feasible because you are trying to create a reference that does not refer to anything. A reference does not create any object, it is merely an alias (alternate name) for an existing object. You could create a reference to an existing array.
Emilio Garavaglia 31-May-11 2:44am    
Richard, the OP asked for an array of references, not a reference of an array (a reference is an alias, so a can THINK to an array of aliases ...)
A reference is a implicitly dereferenced const-pointer. It can technically stay into an array. The problem is that a reference must be initialized with a variable (an L-value) and there is no array initializer for that (array initializer require to be constant, thus references would be "implicily dereferenced const pointer to const" and not "implicitly dereference const pointer to vars" as required).
If I could do that, references will not anymore "not refer to anything".
We are, basically, saying the same thing.
Richard MacCutchan 31-May-11 5:13am    
Yes, I understand what he asked for, but the example code was completely wrong, so I was not sure exactly what problem he was trying to solve. Your suggested language extension could be a way of addressing the issue, but it does not exist at the present time.
nv3 12-Aug-14 9:51am    
My 5, Emilio. I never stumbled across that problem, but your explanation is very plausible.
 
Share this answer
 
Comments
Stefan_Lang 30-May-11 5:51am    
It may just be me, but I consider MSDNs explanation rather lacking: in its attempt to answer a question it raises two new ones:
1. why are pointers to references not allowed
2. how does this make arrays of references impossible?

The first link provides some good arguments, but unfortunately also some inaccuracies, making it difficult to extract the right answer.
Sandeep Mewara 30-May-11 7:49am    
I Agree. :)
Richard MacCutchan 30-May-11 10:27am    
1. Because references are not objects, so you cannot point to them.
2. Same as 1
Stefan_Lang 30-May-11 12:04pm    
1. Yes, that's what I occasionally found, but I've never seen that in any official source, certainly not in Stroustrups "The C++ Programming Language"

2. Doesn't appear all that obvious to me, until you implicitely check out how the symbol defining an array is treated. And even then it's odd - I'd find it more logical if arrays and pointers were separate types alltogether.
Richard MacCutchan 30-May-11 12:25pm    
Sorry, my reply to point 2 was wrong. You can create an array of references, but not in the way that the original question was posed.
You are trying to declare intArrayOfRefs as a reference and also as an array, which makes no sense. A reference is an alias to an existing object, not an object itself. Try stepping through something like the following code in your debugger to see how the references are addressed.
int anInt = 10;          // a simple integer
int *aPointer = &anInt;  // pointer to the integer
int* &refPointer = aPointer;  // reference (alias) to pointer
*anInt = 10;             // set value of anInt
*refPointer = 5;         // change the same variable's value


You also seem to be confusing a reference with the addressof operator; not surprising really.
 
Share this answer
 
v2
Comments
hakz.code 31-May-11 1:28am    
So just to grasp by all this discussion :
->The references can not addressed using the pointer because the address of the reference is not exposed to the world(though i think by an alias it means that it is pointing to an object by holding the address of it in some memory location),
and that makes it not be used as an array as array has the base address that can be pointed to.
->I was not confused by addressof of operator with the intArrayOfRefs identifier as i dont find any similarity in between them.
Richard MacCutchan 31-May-11 5:09am    
You can create a pointer to a reference, after the reference has been created. However in your original question you are trying to create and initialise an array of integers and make it a reference at the same time. You would need to create the references first, then create an array containing either those references, or pointers to them, and finally create a reference to that array. But I'm not sure why you would want to do such a thing.

Re & (reference) and & (addressof) : my point really was that it is a bit confusing that the same character is used for two different operators.
I think you should do like this:

C++
//The Compile Environment is Visual Studio 2008
//array p is the reference of array a

#include <iostream>
using namespace std;

int main()
{  
	int a[]={1,2,3};
	const int length=sizeof(a)/sizeof(int);//length must be const
	int (&p)[length]=a;
	for(int i=0;i<length;++i)>
	{
		cout<<p[i];
		if(i!=length)
		{
			cout<<' ';
		}
	}
	cout<<endl;
	return 0;
}
</iostream>


Good Luck, For your information.
 
Share this answer
 
Comments
Stefan_Lang 13-Aug-14 3:20am    
1. You've changed the meaning of "array of references" to "reference to an array", which is something entirely different.
2. Please don't bother posting solutions to 3 years old threads, unless there is a very good reason (such as the OP adding information and thus reactivating it himself).
Hi,

With reference to knowledge on arrays and references concept i am attempting to answer
the question you asked "Why array of references not allowed in c++". This is just theoretical
& assumption which i am giving.

As we know array is combination of similar data types in a continuous memory,that means
consider the following statement,

int a[4];

Here, with assumption that starting address is 0x1000,

as per array definition,

a[0] will store at 0x1000,
a[1] will store at 0x1004,
a[2] will store at 0x1008,
a[3] will store at 0x1012,


As per definition of reference, its an alias to existing variable, which internally coveys that the address of reference and the aliased variable is same. In that situation if
you consider an array of references as follows,
int a,b,c,d;
int& a[4] = {a,b,c,d};

The memory allocation for this reference view will be as follows again,

a[0] will store at 0x1000 which is alias of a;
a[1] will store at 0x1004 which is alias of b;
a[2] will store at 0x1008 which is alias of c;
a[3] will store at 0x1012 which is alias of d;


As per reference definition a[0] and a should have same address location, and
a[1] and b should have same address location, and so on.... which may not be possible always
as we are not guaranteed that the memory locations of variables which are aliased by array of reference may not be in continuous memory.

From the above assumption we can clearly tell that the array of references are not allowed as we are not guaranteed the address of variables which are aliased by array.

Thank you,

Satya
(I am not perfect, i am making this explanation with assumption and basics of array and references)
 
Share this answer
 
v2
Comments
Stefan_Lang 14-Aug-14 8:27am    
Nobody is perfect, but you should at least check the date of the question you're responding to ;-)
Satya Chamakuri 14-Aug-14 8:31am    
I saw date, why i posted is to share my way of explanation for this question.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900