Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6182767
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T01:15:18+00:00 2026-05-24T01:15:18+00:00

Note: I am using the g++ compiler (which is I hear is pretty good

  • 0

Note: I am using the g++ compiler (which is I hear is pretty good and supposed to be pretty close to the standard).


Let’s say you have declared an array of ints:

int a[3] = { 4, 5, 6 };

Now let’s say you really want to declare a reference to that array (nevermind why, other than the Bjarne says the language supports it).

Case 1 — If you try:

int*& ra = a;

then the compiler balks and says:

"invalid initialization of non-const reference of type `int*&' from a temporary of type `int*'"  

First things first, why is ‘a’ a temporary variable (i.e. doesn’t it have a place in memory?)…

Anyway, fine, whenever I see a non-const error, I try to throw in a const…

Case 2 — if you try:

int*const&rca = a;  //wish I knew where the spaces should go (but my other post asking about this sort of protocol got a negative rank while many of the answers got ranked highly -- aha! there are stupid questions!) 

Then everything is cool, it compiles, and you get a reference to the array.

Case 3 — Now here is another thing that will compile:

int* justSomeIntPointer = a;  //LINE 1
int*& rpa = justSomeIntPointer;  //LINE 2

This also gives you a reference to the original array.

So here is my question: At what point does the name of a statically declared array
become a const-pointer? I seem to remember that the name of an array of ints is also a pointer-to-int, but I don’t remember it ever being a const-pointer-to-int…

It seems like Case 1 fails because the reference declared (ra) is not to a const-pointer, which may mean that ‘a’ was already a const-pointer-to-int to begin with.

It seems like Case 2 works because the reference declared (rca) is already a const-pointer-to-int.

Case 3 also works, which is neat, but why? At what point does the assumed pointer-to-int (i.e. the array name ‘a’) become a const-pointer? Does it happen when you assign it to an int* (LINE 1), or does it happen when you assign that int* to a int*& (LINE 2)?

Hope this makes sense. Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T01:15:20+00:00Added an answer on May 24, 2026 at 1:15 am
    int*& ra = a;
    

    int* is a pointer type, not an array type. So that’s why it won’t bind to a, which has type int[3].

    int* const& ra = a;
    

    works, because it is equivalent to

    int* const& ra = (int*)a;
    

    That is, a temporary pointer is conceptually created on the right-hand side of the assignment and this temporary is then bound to ra. So in the end, this is no better than:

    int* ra = a;
    

    where ra is in fact a pointer to the first element of the array, not a reference to the array.

    Declaring a reference to an array the easy way:

    typedef int array_type[3];
    array_type& ra = a;
    

    The not-as-easy way:

    int (&ra)[3] = a;
    

    The C++11-easy way:

    auto& ra = a;
    

    At what point does the name of a statically declared array become a const-pointer? I seem to remember that the name of an array of ints is also a pointer-to-int, but I don’t remember it ever being a const-pointer-to-int…

    This is the right question to ask! If you understand when array-to-pointer decay happens, then you’re safe. Simply put there are two things to consider:

    • decay happens when any kind of ‘copying’ is attempted (because C doesn’t allow arrays to be copied directly)
    • decay is a kind of conversion and can happen anytime a conversion is allowed: when the types don’t match

    The first kind typically happen with templates. So given template<typename T> pass_by_value(T);, then pass_by_value(a) will actually pass an int*, because the array of type int[3] can’t be copied in.

    As for the second one, you’ve already seen it in action: this happens in your second case when int* const& can’t bind to int[3], but can bind to a temporary int*, so the conversion happens.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.