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 239223
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T20:34:05+00:00 2026-05-11T20:34:05+00:00

I have a piece of code with the following rough signature: void evaluate(object *

  • 0

I have a piece of code with the following rough signature:

void evaluate(object * this)
{
    static const int briefList[] = { CONSTANT_A, CONSTANT_Z };
    static const int fullList[] = { CONSTANT_A, CONSTANT_B, ..., CONSTANT_Z};

    const int const * pArray;
    const int nElements;
    int i;

    if ( this->needDeepsEvaluation ) 
    {
        pArray = fullList;
        nElements = sizeof(fullList) / sizeof(fullList[0]);
    }
    else
    {
        pArray = briefList;
        nElements = sizeof(briefList) / sizeof(briefList[0]);
    }

    for ( i = nElements; i; i-- )
    {
         /* A thousand lines of optimized code */
    }
    this->needsDeepEvaluation = 0;
}

Most compilers will happily swallow the assignment of pArray, but chokes on the assignments of nElements. This inconsistency confuses me, and I would like to be enlightened.

I have no problem accepting that you can’t assign a const integer, but then why does it works as I expect for the const-pointer-to-const?

The fast and cheap fix is to drop the const qualifier, but that might introduce subtle bugs since much of the code inside the loop is macrofied (I’ve been bitten by that once). How would you restructure the above to allow a constant element counter?

  • 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-11T20:34:06+00:00Added an answer on May 11, 2026 at 8:34 pm

    In your declaration of pArray

    const int const * pArray;
    

    Both ‘const’ keywords actually apply to int. To get one to apply to the pointer, you’d have to declare it as int const * const pArray, in which the pointer itself becomes immutable. Your compiler should then throw an error on both assignments.

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

Sidebar

Related Questions

I have following piece of code: private void nameTextBox_Leave(object sender, EventArgs e) { var
I have following piece of code: customObject* object; std::list<customObject> objects; for(int i = 0;
I have following piece of code: typedef uint8_t array_t[8]; static array_t _my_array; static const
I have this piece of code in my Silverlight project: private void button1_Click(object sender,
I have this piece of code from GWT in Action: public void processOperator(final AbstractOperator
I have following piece of code: public void ProcessRequest (HttpContext context) { context.Response.ContentType =
I have a piece of code like follows public class Test{ public static void
I have following piece of code: class Test{ private: int id; public: Test(int v):id(v)
I have a piece of code which is similar to the following: final int
I have following piece of code: It compiles without problems under gcc-3.4, gcc-4.3, intel

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.