I am aware that it is possible to declare constants using the #define
macro. With this, it would be simple to define integer, floating point or character literals to be constants.
But, for more complicated data structures, such as an array, or a struct, say for example:
typedef struct {
int name;
char* phone_number;
} person;
I want to be able to initialise this just once and then make it a non-editable struct.
In object oriented languages, there exists the final
keyword to do this easily, but there is no such thing in C. One workaround I've thought of is to use setjmp
and longjmp
to simulate try-catch braces and do a rollback if a change is detected. You'd need to store a backup in a file/in memory object, which can get little messy if you have many such objects you want to protect from accidental alteration.