CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Jan 2007
    Posts
    143

    Red face Array of references

    Hello Guys,

    why array of references are not allowed,can anyone give me explanation on this.

    thanks in advance

  2. #2
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    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.

  3. #3
    Join Date
    Jan 2007
    Posts
    143

    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;
    }

    }

  4. #4
    Join Date
    Jan 2009
    Posts
    1,689

    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.

  5. #5
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Array of references

    Quote Originally Posted by resumurof View Post
    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.

  6. #6
    Join Date
    Apr 2008
    Posts
    118

    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?

    Code:
    &int pnArray2[10];
    Last edited by Moschops; November 5th, 2010 at 10:35 AM.

  7. #7
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    Re: Array of references

    Quote Originally Posted by resumurof View Post
    void main()
    That's int main() !

    Quote Originally Posted by Moschops View Post
    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?

    Code:
    &int pnArray2[10];

    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.

  8. #8
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Array of references

    Quote Originally Posted by resumurof View Post
    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

  9. #9
    Join Date
    Apr 2004
    Location
    England, Europe
    Posts
    2,492

    Re: Array of references

    Quote Originally Posted by resumurof View Post
    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.
    My hobby projects:
    www.rclsoftware.org.uk

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured