Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C/C++ changing the value of a const

Tags:

I had an article, but I lost it. It showed and described a couple of C/C++ tricks that people should be careful. One of them interested me but now that I am trying to replicate it I'm not being able to put it to compile.

The concept was that it is possible to change by accident the value of a const in C/C++

It was something like this:

const int a = 3;          // I promise I won't change a
const int *ptr_to_a = &a; // I still promise I won't change a
int *ptr;
ptr = ptr_to_a;

(*ptr) = 5;               // I'm a liar; a is now 5

I wanted to show this to a friend but now I'm missing a step. Does anyone know what's missing for it to start compiling and working?

ATM I'm getting invalid conversion from 'const int*' to 'int*' but when I read the article I tried and it worked great.

like image 583
fmsf Avatar asked Feb 24 '09 18:02

fmsf


People also ask

Can we change value of const in C?

In C or C++, we can use the constant variables. The constant variable values cannot be changed after its initialization.

Can you change the value of a const?

Constants are block-scoped, much like variables declared using the let keyword. The value of a constant can't be changed through reassignment (i.e. by using the assignment operator), and it can't be redeclared (i.e. through a variable declaration).

How do I change the value of a const integer?

Changing Value of a const variable through pointerBy assigning the address of the variable to a non-constant pointer, We are casting a constant variable to a non-constant pointer. The compiler will give warning while typecasting and will discard the const qualifier.

Can you change a const char in C?

In C programming language, *p represents the value stored in a pointer and p represents the address of the value, is referred as a pointer. const char* and char const* says that the pointer can point to a constant char and value of char pointed by this pointer cannot be changed.

How to modify a const variable in C/C++?

How to modify a const variable in C? In C or C++, we can use the constant variables. The constant variable values cannot be changed after its initialization. In this section we will see how to change the value of some constant variables. If we want to change the value of constant variable, it will generate compile time error.

How to change the value of a const variable through pointer?

Changing Value of a const variable through pointer. The variables declared using const keyword, get stored in .rodata segment, but we can still access the variable through the pointer and change the value of that variable. By assigning the address of the variable to a non-constant pointer, We are casting a constant variable to a non-constant ...

Is it possible to change a const by accident in C/C++?

The concept was that it is possible to change by accident the value of a const in C/C++ const int a = 3; // I promise I won't change a const int *ptr_to_a = &a; // I still promise I won't change a int *ptr; ptr = ptr_to_a; (*ptr) = 5; // I'm a liar; a is now 5 I wanted to show this to a friend but now I'm missing a step.

How to change the value of X (which is a constant)?

Now we will see how we can change the value of x (which is a constant variable). To change the value of x, we can use pointers. One pointer will point the x. Now using pointer if we update it, it will not generate any error.


1 Answers

you need to cast away the constness:

linux ~ $ cat constTest.c
#include <stdio.h>


void modA( int *x )
{
        *x = 7;
}


int main( void )
{

        const int a = 3; // I promisse i won't change a
        int *ptr;
        ptr = (int*)( &a );

        printf( "A=%d\n", a );
        *ptr = 5; // I'm a liar, a is now 5
        printf( "A=%d\n", a );

        *((int*)(&a)) = 6;
        printf( "A=%d\n", a );

        modA( (int*)( &a ));
        printf( "A=%d\n", a );

        return 0;
}
linux ~ $ gcc constTest.c -o constTest
linux ~ $ ./constTest
A=3
A=5
A=6
A=7
linux ~ $ g++ constTest.c -o constTest
linux ~ $ ./constTest
A=3
A=3
A=3
A=3

also the common answer doesn't work in g++ 4.1.2

linux ~ $ cat constTest2.cpp
#include <iostream>
using namespace std;
int main( void )
{
        const int a = 3; // I promisse i won't change a
        int *ptr;
        ptr = const_cast<int*>( &a );

        cout << "A=" << a << endl;
        *ptr = 5; // I'm a liar, a is now 5
        cout << "A=" << a << endl;

        return 0;
}
linux ~ $ g++ constTest2.cpp -o constTest2
linux ~ $ ./constTest2
A=3
A=3
linux ~ $

btw.. this is never recommended... I found that g++ doesn't allow this to happen.. so that may be the issue you are experiencing.

like image 122
sfossen Avatar answered Sep 18 '22 17:09

sfossen