-2

Recently I learnt I could modify a constant using pointers in C.

I have successfully achieved this if the constant is defined in the same source file where we are trying to update the constant. However, consider we have two files:

File1.c

int const age=34;

File2.c


#include "File1.c"
#include <stdio.h>
#include <stdlib.h>

extern int const age;

int main(void){

       int newAge=2*age;

       int *ptrAge=(int *) &age;
       *ptrAge= newAge;

       printf("Modified age is %d", age);
        
       return 0;
}

This does not seem to compile because I am getting an error saying: "The instruction [some address] referenced memory at [some address]. the memory could not be written".

Does anyone know how can we change a constant defined in another file?

Many thanks

Oriol
  • 75
  • 5
  • 3
    If you could change it then it wouldn't be a constant would it? – Retired Ninja Sep 03 '21 at 11:26
  • 1
    modifying an object that was created as const results in UB. You don't want that.. – tstanisl Sep 03 '21 at 11:27
  • Generally you do not want to change them by accident, but constants can be modified if needs be using pointers as shown here. Though I am not sure if this happens exactly like this if the constant was defined elsewhere. – Oriol Sep 03 '21 at 11:27
  • _Recently I learnt I could modify a constant using pointers in C_ : actually you just learned that constant objects cannot be modified. Or precisely: trying to modify a constant object results in undefined behaviour, that is on some platforms you may get away with it, but not on others – Jabberwocky Sep 03 '21 at 11:28
  • 1
    *"constants can be modified if needs be using pointers as shown here"* - no, that's undefined behavior. Don't do it. The fact that it seems to work does not mean that it is a legal operation. *"Does anyone know how can we change a constant defined in another file?"* - you cannot. It's a ***constant***. – Marco Bonelli Sep 03 '21 at 11:33
  • Note that the fact that `age` is in a different file here is a bit of a red herring. Because you're `#include`-ing the other file, the definition of `age` is in the same translation unit as main. But whether it's in the same translation unit or not, as other people have pointed out modifying a const via pointer casts is undefined behavior. – Paul Hankin Sep 03 '21 at 11:35
  • you could use a pointer to *const* object to access *non-const* object. Casting such a pointer to non-const one would allow changing the original object without UB. However, it is not this case because `age` is created a const object. – tstanisl Sep 03 '21 at 11:35
  • 1
    Does this answer your question? [Confusion regarding modification of const variable using pointers](https://stackoverflow.com/questions/21372713/confusion-regarding-modification-of-const-variable-using-pointers) – Yun Sep 03 '21 at 11:37
  • @RetiredNinja: That would be true if constants were constant, but C has many types of constants that differ in when they may be changed, including literals (value is stated in their source text, like “34”), lexical constants (value is derived from source text but may vary between C implementations, like `'a'`), enumeration constants, arithmetic constant expressions, integer constant expressions, constant expressions in initializers, address constants, objects defined as `const`, and lvalues that are `const` but refer to objects not defined as `const`. – Eric Postpischil Sep 03 '21 at 12:05
  • @RetiredNinja: Among these, there are constants that can never be changed (except by changing their meaning in the C standard), constants that can be changed when the C implementation is changed, constants that can be changed up to compile time, constants that can change at link time, constants that can be changed when the program starts running, constants that can be changed when execution reaches a definition inside a block, and constants that can change when the program is running. So it is perfectly normal that a student might have questions about when and how a constant can change. – Eric Postpischil Sep 03 '21 at 12:06
  • 1
    why all the DVs for this question? OP asked a clear, focused, specific question with a [mre] to illustrate it. Only complaint I could see is lack of research, but if OP began with the premise that this was ok, may not find the right stuff googling. Besides, there are plenty of dupes on SO with hundreds of UVs. – yano Sep 03 '21 at 12:11
  • 1
    @yano My guess is that OP fell victim for users who are tired of seeing the same questions over and over again. :) – klutt Sep 03 '21 at 12:14

1 Answers1

4

Recently I learnt I could modify a constant using pointers in C.

False

I have successfully achieved this if the constant is defined in the same source file where we are trying to update the constant.

You did not successfully do it. You invoked undefined behavior. And since undefined behavior means the behavior is not defined, it may work the way you intended. Maybe it crashes the next time you execute it. Or if you recompile it with other flags, or another version, or run it on another computer. Etc...

What is Undefined Behaviour in C?

klutt
  • 30,332
  • 17
  • 55
  • 95