Thread: refrence to an array of int?

  1. #1
    Registered User
    Join Date
    Jul 2003
    Posts
    450

    refrence to an array of int?

    Bjarne Stroustrup asks in C++ Programming Language 3rd edition exercise 5.9 1. to declare a refrence to an array of 10 integers. Is this possible. I know you can declare pointer to an array but a refrence?

    Code:
    int inumbers[10];
    int *pinumbers = inumbers;  //pointer to array
    
    int &rinumbers= inumbers[0];// refrence to first element;
    
    int &rinumber=inumbers;// compiler error

  2. #2
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Code:
    int &rinumber=inumbers;// compiler error
    The reason for the error is that you are trying to assign to a reference to an int, not an int* (i.e. it is roughly equivalent to 'char& r = inumbers', the types are incompatible). To create the reference, you need:
    Code:
    int* &rinumber=inumbers;// compiler error
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  3. #3
    Registered User
    Join Date
    Jul 2003
    Posts
    450
    ok thanks thought I was going bonkers before that explanation was under the assumption from the compiler error that you could not initialize a refrence with a pointer.

    I still get the compiler error:
    declare.cc:17: error: invalid initialization of non-const reference of type '
    int*&' from a temporary of type 'int*'

    with
    Code:
    int inumbers[10];
    int * &rinumbers=inumbers;
    Last edited by curlious; 12-12-2003 at 11:45 PM.

  4. #4
    Registered User
    Join Date
    May 2003
    Posts
    161
    Use this instead:

    Code:
    int * const & rnumbers = inumbers;
    The reason you need a const is that an array cannot be modified. It would be illegal to take the above reference and increment it (as this would modify the location of the array).

    By the way, which compiler are you using? I don't agree with the error message that you're receiving. It seems like it's reached a default case in trying to diagnose a failure to initialize a reference.
    Last edited by thefroggy; 12-13-2003 at 12:14 AM.

  5. #5
    Registered User
    Join Date
    Jul 2003
    Posts
    450
    I am using g++ under linux, I am not sure what version, but am fairly sure it is up to date as I am using fedora.

    I didn't realize you could increment a refrence anyhow like pointer arithmatic.

    I assume I could use the refrence in the form
    rinumbers[index]=10;
    correct?

    I am going to go implement your solution and see what the compiler tells me. Hopefully nothing!
    Last edited by curlious; 12-13-2003 at 12:30 AM.

  6. #6
    Registered User
    Join Date
    May 2003
    Posts
    161
    > I didn't realize you could increment a refrence anyhow like pointer arithmatic.

    You can if you're referencing a pointer-- but not an array. But remember that a reference is just an alias. If you reference a pointer and then increment it, the pointer itself will also change. This is the reason why a reference to an array must be a reference to const: you are not allowed to change the location of an array.


    > I assume I could use the refrence in the form

    Yes, that will work as you would expect.

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >>to declare a refrence to an array of 10 integers
    >int * const & rnumbers = inumbers;<and similar answers>
    You realize that every solution posted already fails to meet the requirement? All of these are references to pointers, not references to arrays. To get a true reference to an array, you would do this:
    Code:
    int array[10];
    int (&ra)[10] = array;
    My best code is written with the delete key.

  8. #8
    Registered User
    Join Date
    Jul 2003
    Posts
    450
    Interesting Prelude, looking at the solutions closely I see the difference. Even what is supposed to be basic declarations can be difficult. My first attempt at the declaration was way off. In my attempt I forgot that an array name is both a pointer (though I intuitivly new this when using another pointer) and that it is constant. What threw me is my use of a pointer r, which I assumed would be similar with refrence, but like you explained a refrence is just an alias and a refrence to a constant pointer in the case of an array.

    We all missed the fact that now we had a refrence to a constant pointer, and not actually to an array. The only difference I could see in the result between your solution and previous solutions is in the use of pointer aritmatic which, I am guessing is forbidden with your solution. I will go try it and check. No I was wrong of course. A refrence to a const pointer cant be changed nor can preludes soltution. doh.

    I think I have a better grip on both pointers, arrays and refrences because of everyones help and this little exercise thanks. (Of course I thought I knew it all before trying this
    Last edited by curlious; 12-13-2003 at 10:42 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 03-10-2008, 11:57 AM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. problem with sorting
    By pinkpenguin in forum C Programming
    Replies: 2
    Last Post: 11-18-2005, 11:06 AM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  5. easy if you know how to use functions...
    By Unregistered in forum C Programming
    Replies: 7
    Last Post: 01-31-2002, 07:34 AM