How to reference an array

I just bought "The C++ programming language" By Stroustrup and one of the exercises in it says to reference an array of 10 integers. Well here is the code I have but it doesn't work
1
2
3
4
5
6
7
8
9
10
11
12
13
     
    //reference to an array of 10 integers
  
    cout << endl << endl << "Values in nArray2 when referenced" << endl;
    
    int nArray2[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
    
    int& pnArray2 = nArray2[10];
    
    for(;pnArray2 < 10; pnArray2++)
    {
                  cout << pnArray2 << endl;
    }


The code compiles but the count starts ouputing at -2 and it doesn't output the referenced array.
I'm not very good with refrences, but is int& pnArray2 = nArray2[10]; correct? It seems like it would just get the refrence of the value at 10 and probably screw up the rest of your code.
firedraco is right. References can't be incremented. Only the value the reference.
In any case, initializing it to nArray2[10] doesn't make much sense. You'd have to initialize it to nArray2, but then it'd be easier to just use the pointer directly.

EDIT: Oh, yeah. And there are better books out there for learning C++ than The C++ Programming Language.
Last edited on
The OP got his array indexing mixed up.

However, I don't think you can make a reference of an array directly.


You can do something like this though:
1
2
3
4
5
6
        int nArray2[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
    
    int* middleman = nArray2;

    int*& andy = middleman;
    
I didn't know you couldn't reference an entire array. I will try that reference to pointer method.
@helios Other books I've read teach the language in a bad order and are not indepth enough.
If your only goal is to output the contents of the array, then I'd suggest forgoing references and just do it like this:

1
2
for( size_t i = 0; i < sizeof( nArray2 ); ++i )
   cout << nArray2[ i ] << endl;


jssmith ...i don't think you can get the size of an array with 'sizeof'
Last edited on
You can when the array is not dynamic.
Although you get the size in bytes. You should use sizeof(nArray2)/sizeof(int)
Last edited on
Oops, yes, thanks for the fix helios.

Typically I use the template trick for this:

1
2
3
4
5
template< typename T, size_t N >
size_t ArraySize( T (& const)[ N ] )
{
    return N;
}


Edit: some older compilers (namely some versions of GCC) have issues with the parameter to this function and/or fail to match the const properly so sometimes you also have to declare the non-const version too:

1
2
3
4
5
template< typename T, size_t N >
size_t ArraySize( T (&)[ N ] )
{
    return N;
}

Last edited on
I don't really see the point of doing that. The size is known at all times since it's constant. You might as well use that.
The reason for using the template method is that it is hard(er) to get wrong. As opposed to the sizeof(array)/sizeof(array[0]) implementation which my original post clearly demonstrated is easily possible to get wrong :)
I meant the point in getting the size at all, as opposed to just using the constant (10, in this case. Or DEF_BUFFER_SIZE for example).
Last edited on
Yeah, this example is trivial enough that it's just easier to use the constant.

For a large app I'd use the template method as it avoids duplicating the constant (maintainability) and avoids having to use a #define.
Well I think we have got away from the original posters question/problem.

An excercise in Stroustrup's book does the reader to declare various references, and one of them is a reference to an array.

Is it possible ( I think it may be trick question, and you can't declare such a reference).
Okay thank you for the help. I was just wondering if it could be done and how to do it. Maybe I Stroustrup worded the question wrong and he meant pointer. I know how to access arrays through pointers.
To declare a reference to an array:

1
2
int array[ 10 ];
int (&array_ref)[ 10 ] = array;    // array_ref is now a reference to array 



The parentheses are required because otherwise the declaration

 
int &array_ref[ 10 ];


would attempt to declare an array of 10 references to ints.

yes jsmith that works.
I thought I had tried every which way
I have saw the parentheses version of referencing and pointers but never paid much attention to it. Thank you all for clarifying everything.
Topic archived. No new replies allowed.