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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T08:02:39+00:00 2026-06-13T08:02:39+00:00

//Example 1 const double pie = 3.14; // const object const double *cptr =

  • 0
//Example 1
const double pie = 3.14;   // const object
const double *cptr = &pie; // pointer-to-const to const object
double *ptr = &pie;        // ERROR - pointer-to-non-const to const

//Example 2
double pie = 3.14;         // non-const object
const double *cptr = &pie; // pointer-to-const to non-const object
double *ptr = &pie;        // pointer-to-non-const to non-const object

Initially I though pointer-to-const to non-const object is allowed because it just mean
pointer-to-const won’t change non-const object.

But i just read in c++ book
that the reason pointer-to-const to non-const object is allowed because there’s no
way for pointer-to-const pointer to really know whether the object it points to is const,
so it treat the object it points as const
but by the same logic, pointer-to-non-const would treat const object as non-const object
but compiler will throw error at compile time.

Am I missing something here?

  • 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-06-13T08:02:40+00:00Added an answer on June 13, 2026 at 8:02 am

    A pointer-to-const

    const double *p
    

    treats the object it points to as a constant object, i.e. it won’t allow you to change it. As soon as you dereferentiate the pointer

    *p
    

    you are dealing with a constant object. If you try to modify that,

    *p = 1.0;
    

    you will get an error message from the compiler.

    On the other hand, a pointer-to-non-const,

    double *p
    

    treats the object it points to as non-const and therefore allows you to modify its value:

    *p = 1.0;
    

    becomes legal. However, if at any given time you have a constant object

    const double d = 1.0;
    

    you won’t be able to create a pointer-to-non-const that points to it:

    double *p = &d;
    

    won’t be accepted by the compiler. So yes, the compiler enables a pointer-to-non-const to modify the objects it points to. But it doesn’t enable you to create a pointer-to-non-const that points to a constant object. So you won’t run into a situation where a pointer-to-non-const can be used to modify a constant object.

    Regarding the question in the title: This is primarily a language design decision. However, the fact that the compiler can rely on certain objects to never change value means that various optimizations are possible. For example, if the same pointer is dereferentiated twice in a function with other code between the two dereferentiations, and if that pointer is a pointer-to-const, the compiler will assume it does not actually have to fetch the value from memory twice, because the value is not allowed to have changed between the first and the second dereferentiation.

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

Sidebar

Related Questions

In the following example: void foo (double *ptr) { const double * restrict const
Example: I have a function that works with vectors: double interpolate2d(const vector<double> & xvals,
for example: Beta_ab&& Beta::toAB() const { return move(Beta_ab(1, 1)); }
Why does: const char example; (uint64*)example have a value of 140734799798420 and *(uint64*)example have
For example, does Apple provide NSString * const kCFBundleDisplayName somewhere? Or do I have
Given the next code example, I'm unable to free the parameter const char* expression
I have a string as const char *str = Hello, this is an example
I've got following (simplified for example purpose) code and it works: void log(const string
In the following example, I tried to use uninterpreted Boolean function like (declare-const p
why i need to use the const function in the less traits? for example,

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.