-5
#include<stdio.h>
void main() {
  const int convalue=64;
  int *point;
  point = &convalue;
  (*point)++;
  printf("address of point is %p address of convalue is %p\n",point,&convalue);
  printf("convalue is %d and point is %d\n",convalue,*point);
}

How can i modify the constant variable value

jaswanth
  • 29
  • 1
  • 1
  • 8
  • 2
    You can't and shouldn't modify the value of a `const` variable. – Jason Dec 27 '21 at 10:25
  • Is this for C or C++? Choose one language - both C and C++ are different languages technically. – kiner_shah Dec 27 '21 at 10:27
  • Depending on your platform, the compiler might place `convalue` in a read-only memory section of the executable image. Subsequently, during runtime, your memory-write operation will cause a memory-access violation. –  Dec 27 '21 at 10:28
  • 1
    Does this answer your question? [Can we change the value of an object defined with const through pointers?](https://stackoverflow.com/questions/3801557/can-we-change-the-value-of-an-object-defined-with-const-through-pointers) – Mark Benningfield Dec 27 '21 at 10:58
  • Why do you wish to do that? – Cosinus Dec 27 '21 at 11:32

3 Answers3

2

How can i modify the constant variable value

You cannot change the value of a constant1 after initialisation. That's what "constant" means.

You can change the program by removing the const qualifier. Then you can change the value. Or, you can conditionally choose another value during initialisation.

1 Except if the constant is of class type, then you can change mutable members of the object.


void main()

The pogram is ill-formed. main must return int in C++. It may not return void.

point = &convalue;

The pogram is ill-formed. A pointer to const doesn't implicitly convert to pointer to non-const. The type system of the language protects you from making mistakes such as this.

(*point)++;

The behaviour of the program is undefined. Const objects must not be modified. This is a bug. Don't write this in a program.

eerorika
  • 232,697
  • 12
  • 197
  • 326
1

We can't and should never try to modify the value of a const variable. Also,

Any attempt to modify a const object results in undefined behavior.

Also,

note that undefined behavior means anything can happen including but not limited to program being successfully compiled and giving your expected output. But don't rely on the output of a program that has undefined behavior.

For more reading on undefined behavior you can refer to undefined behavior's documentation which mentions:

there are no restrictions on the behavior of the program.

Mistake 2

There is another mistake(in addition to undefined behavior) in your program(in C++). In particular, you should replace

int *point;//INCORRECT

with

const int *point;//CORRECT, note i have added const here

Mistake 3

You should replace void main() with

int main()

Because void main() is not standard C++ as Bjarne Stroustrup made this quite clear in this article

The definition void main() is not and never has been C++, nor has it even been C.

Jason
  • 36,170
  • 5
  • 26
  • 60
  • Yeah but it works in gcc – jaswanth Dec 27 '21 at 10:30
  • @jaswanth As i said, **undefined behavior** means anything can happen including but not limited to program being successfully compiled. That is you still have undefined behavior in your program and you should not rely on its output. – Jason Dec 27 '21 at 10:33
  • In the C standard “undefined behavior” does not mean anything can happen. It means the C standard does not impose any requirements on what happens. The difference is the C standard does not remove requirements imposed by other things, including operating systems, hardware specifications, product safety laws, physics, or logic. The requirements of these other things remain, preventing “anything” from happening. – Eric Postpischil Dec 27 '21 at 10:40
  • `void main()` is often used in embedded programming, where returning from the main function makes no sense. – Cosinus Dec 27 '21 at 11:29
  • @Cosinus `void main()` is not standard C++. In particular, A conforming implementation may provide more versions of main(), but they must all have return type int. The int returned by `main()` is a way for a program to return a value to “the system” that invokes it. On systems that don’t provide such a facility the return value is ignored, but that *doesn’t make* `void main()` legal C++ or legal C. Even if your compiler accepts void main() *avoid it* in any case. It's incorrect. – Jason Dec 27 '21 at 11:32
  • This question is about C, not C++. C11 standard (section 5.1.2.1) says: "In a freestanding environment (in which C program execution may take place without any benefit of an operating system), the name and type of the function called at program startup are implementation-defined", so `void main()` can be legal. – qrdl Dec 27 '21 at 12:30
  • @qrdl The OP originally tagged both C++ and C and i answered according to that. Also, it is not my fault that someone later removed the tag C++ from the answer. You can look up in the question history for confirming this. – Jason Dec 27 '21 at 12:47
0

Simply – you can't. "Constant" is constant.
If you need to, you have to use variable instead.

Anyway, there is one possibility to get what you (maybe) want – preprocessor. Question is a bit strange, so I think you could be satisfied by that.

Using preprocessor you can determine what is the initial value of const. (And more, but that's not this case.) Still you can't modify it after it's initialized, but maybe it will be enough for you. For example:

#include<stdio.h>
void main() {

  const int convalue=
  #ifdef CLI_PROVIDED
    CLI_PROVIDED;
  #undef CLI_PROVIDED
  #else
    64;
  #endif

  int *point;
  point = &convalue;
  (*point)++;
  printf("address of point is %p address of convalue is %p\n",point,&convalue);
  printf("convalue is %d and point is %d\n",convalue,*point);
}

Normal compilation will initialize const with value 64. Compilation with option g++ -DCLI_PROVIDED=123 will modify initial value of const BEFORE compilation (this is just text operation) and then compile it with value 123. Preprocessor is working with code before compilation.

If you want to (for example) create program for multiple operating systems, it will be useful.

Sylogista
  • 565
  • 3
  • 10