Is there any way to change the value of constant variables during program execution in C? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Is there any way to change the value of constant variables during program execution in C?

I LOVE C PROGRAMMING 😘

31st May 2018, 4:20 AM
💫...MayurK Koli...💫
💫...MayurK Koli...💫 - avatar
6 Answers
+ 4
Yes, but it is not enough to use a pointer as it has an undefined behaviour (meaning that different compilers may give you different results). You have to instead cast the pointer to int* and dereference it again. I'll explain better with code: const int c = 10; *(int *)&c = 0; printf("c is %d", c); //c is 0
31st May 2018, 5:54 AM
Stefano Prosperi
Stefano Prosperi - avatar
+ 2
hey dear i know all this stuff i just post a question because I didn't before 😂 and ya the value of const var can be change by using pointer which pointing to that var but the pointer must not be a const .
31st May 2018, 5:45 AM
💫...MayurK Koli...💫
💫...MayurK Koli...💫 - avatar
+ 2
Jan Markus thank god, that u understand me in such a short sentences
31st May 2018, 5:50 AM
Nura Programmer
Nura Programmer - avatar
+ 2
Stefano Prosperi 👏👍, but he is asking if there is a Way,,,
31st May 2018, 5:58 AM
Nura Programmer
Nura Programmer - avatar
+ 2
thanks stefano for such a great info really clear out all my doubt.
31st May 2018, 5:59 AM
💫...MayurK Koli...💫
💫...MayurK Koli...💫 - avatar
+ 1
yes, technically u can use pointer; make it point to the value and replace it👍 guest u need much more lesson on using them😨😨😨😨 hmm, just kidding..
31st May 2018, 4:43 AM
Nura Programmer
Nura Programmer - avatar