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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T14:17:43+00:00 2026-05-11T14:17:43+00:00

I have the following piece of code: void TestFunc(const void * const Var1, const

  • 0

I have the following piece of code:

 void TestFunc(const void * const Var1, const float Var2) {   *(float*)Var1 = Var2; } 

It looks like I am changing the value of the const object the const pointer points to (thanks sharptooth), which should not be allowed. Fact is, none of the compilers I tried issued a warning. How is this possible?

  • 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. 2026-05-11T14:17:43+00:00Added an answer on May 11, 2026 at 2:17 pm

    As others mentioned, the cast removes the ‘constness’ of the destination as far as the expression is concerned. When you use a cast the compiler treats the expression according to the cast – as long as the cast itself it valid (and C-style casts are pretty much the big hammer). This is why you don’t get an error or warning. You’re essentially telling the compiler, ‘be quiet, I know what I’m doing, this is how you should treat things’. In fact, casts are probably the #1 way for programmers to get the compiler to stop issuing warnings.

    Your assignment expression may or may not be undefined behavior. It is permitted to cast away constness if the object actually pointed to is not const.

    However, if the object pointed to is const, then you have undefined behavior.

    void TestFunc(const void * const Var1, const float Var2) {   *(float*)Var1 = Var2; }   int main(void) {     float x = 1.0;     const float y = 2.0;      TestFunc( &x, -1.0);    // well defined (if not particularly great style)     TestFunc( &y, -2.0);    // undefined behavior      return 0; } 

    You’re treading dangerous waters…

    In general (I’m sure there are exceptions), casting so that expressions treat objects as they really are is supported, well-defined behavior in C/C++.

    This particular behavior is covered in the standards mostly by statements that modifying a const object through a cast (or something) that removes the const qualifier is undefined. The inference is that doing the same for a non-const object is not undefined. An example given in the C++ standard makes this clear.

    C90 6.5.3 – Type Qualifiers (C99 6.7.3):

    If an attempt is made to modify an object defined with a const-qualified type through use of an lvalue with non-const-qualified type, the behavior is undefined.

    C++ 7.1.5.1 The cv-qualifiers

    A pointer or reference to a cv-qualified type need not actually point or refer to a cv-qualified object, but it is treated as if it does; a const-qualified access path cannot be used to modify an object even if the object referenced is a non-const object and can be modified through some other access path. [Note: cv-qualifiers are supported by the type system so that they cannot be subverted without casting (5.2.11). ]

    Except that any class member declared mutable (7.1.1) can be modified, any attempt to modify a const object during its lifetime (3.8) results in undefined behavior.

    …

    [Example:

    …

    int i = 2;            //not cv-qualified const int* cip;       //pointer to const int cip = &i;             //OK: cv-qualified access path to unqualified *cip = 4;             //ill-formed: attempt to modify through ptr to const  int* ip; ip = const_cast<int*>(cip);   //cast needed to convert const int*to int* *ip = 4;                      //defined: *ip points to i, a non-const object  const int* ciq = new const int (3);   //initialized as required int* iq = const_cast<int*>(ciq);      //cast required *iq = 4;                              //undefined: modifies a const object 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 123k
  • Answers 123k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer it was probably some combination of updates/plugins installed. after last… May 12, 2026 at 1:10 am
  • Editorial Team
    Editorial Team added an answer This will definitely eat your battery real quick. You never… May 12, 2026 at 1:10 am
  • Editorial Team
    Editorial Team added an answer It does depend on the way that you are accessing… May 12, 2026 at 1:10 am

Related Questions

I have the following piece of code pattern: void M1(string s, string v) {
I have the following piece of code to write data to an XML file.
Hy all, I believe that the following piece of code is generating memory leak?
I have the following code: #include <string.h> int main(void) { char *buffer = NULL,

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.