Is there any way of protecting a variable for being modified at runtime in C? -
i wondering if there way of protecting variable being modified once initialized (something "constantize" variable @ runtime ). example:
#include <stdio.h> #include <stdlib.h> int main(void) { int v, op; scanf( "%d", &op ); if( op == 0 ) v = 1; else v = 2; // here transform v constant... . . // ...and attempt modify v yields error. . . return exit_success; }
you can make result of input const this:
int func() { int op = 0; scanf( "%d", &op ); if( op == 0 ) return 1; else return 2; } int main() { const int v = func(); // ... } nb. of course, there no way prevent undefined behaviour happening later in program appears change v (since undefined behaviour can, definition, have effect).
Comments
Post a Comment