|
-
November 5th, 2010, 10:02 AM
#1
Array of references
Hello Guys,
why array of references are not allowed,can anyone give me explanation on this.
thanks in advance
-
November 5th, 2010, 10:07 AM
#2
Re: Array of references
Because the standard does not require a reference to have a size in memory, and array elements must have a defined size.
In practice most compilers implement references as pointers, which have a size, but there are other possibilities.
-
November 5th, 2010, 10:18 AM
#3
Re: Array of references
I have a sample program created for array of references, it works fine.
void main()
{
cout << endl << endl << "Values in nArray2 when referenced" << endl;
int pnArray2[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int (&array_ref)[ 10 ] = pnArray2;
for(int i=0;i < 10; i++)
{
cout << array_ref[i] << endl;
}
}
-
November 5th, 2010, 10:23 AM
#4
Re: Array of references
Some compilers support other things that are not in the standard, GNU seems to allow arrays of references. Another example of compiler specific functionality is your main method. G++ wont compile unless main returns int.
-
November 5th, 2010, 10:24 AM
#5
Re: Array of references
 Originally Posted by resumurof
I have a sample program created for array of references, it works fine.
I believe that's a reference to an array, not an array of references.
Last edited by Lindley; November 5th, 2010 at 10:30 AM.
-
November 5th, 2010, 10:28 AM
#6
Re: Array of references
Looks like an array of ints to me, not an array of references. What happens if you try to make an array of references to ints, like this?
Last edited by Moschops; November 5th, 2010 at 10:35 AM.
-
November 5th, 2010, 10:48 AM
#7
Re: Array of references
 Originally Posted by resumurof
void main()
That's int main() ! 
 Originally Posted by Moschops
Looks like an array of ints to me, not an array of references. What happens if you try to make an array of references to ints, like this?
Yes. The correct syntax to incorectly create an array of references is:
Code:
int &(array_ref[10]) = ...;
or
int &array_ref[10] = ...;
Both of which are diagnosed as:
Code:
error: declaration of 'array_ref' as array of references
Last edited by monarch_dodra; November 5th, 2010 at 10:53 AM.
Is your question related to IO?
Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.
-
November 5th, 2010, 11:04 AM
#8
Re: Array of references
 Originally Posted by resumurof
I have a sample program created for array of references,
Code:
int (&array_ref)[ 10 ] //...
When was the last time you coded an array declared like that, with all of those parentheses? Probably never. Wouldn't this be much easier?
Code:
int &array_ref[10];
But this doesn't compile, so this means that there is no such thing as an "array of references". This is from the on-line Comeau compiler:
Code:
Thank you for testing your code with Comeau C/C++!
Tell others about http://www.comeaucomputing.com/tryitout !
Your Comeau C/C++ test results are as follows:
Comeau C/C++ 4.3.10.1 (Oct 6 2008 11:28:09) for ONLINE_EVALUATION_BETA2
Copyright 1988-2008 Comeau Computing. All rights reserved.
MODE:strict errors C++ C++0x_extensions
"ComeauTest.c", line 12: error: array of reference is not allowed
int &array_ref[ 10 ] = pnArray2;
Your original declaration is not an "array of references". It is a single reference to an array of 10 integers. If you tried to assign an array with a different number of ints, you get an error.
Regards,
Paul McKenzie
-
November 6th, 2010, 08:58 AM
#9
Re: Array of references
 Originally Posted by resumurof
why array of references are not allowed
You cannot take the address of a reference, but arrays in C++ are really just pointers to the first element of an array.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|