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