How do you specify variables to be final in C?

12,610

Solution 1

Use const as keyword for variable. This is a way how to prevent value to be modified later.

const int a = 5;
a = 7; //Error, you cannot modify it!

For example, in embedded systems where you have flash memory, this variable may be put to flash by linker, if available. But it is not necessary the case.

Solution 2

const is the way to go. const is a keyword that might tell the compiler two things:

  1. Enforce the constantness of an object.

    const YourType t;
    

    Declares a non-modifiable object. The compiler will force the const-correctness. It is important to note that the const-correctness is enforced conceptually by the compiler and there are ways to elude those rules.

  2. constantness of pointer (or to an access to an object)

    const int* pointer
    

    Declares a const pointer to an int (which is not const). It means that if there is a non-const pointer to that int then it can be modified.

For more info see this great answer.

Solution 3

The equivalent keyword in C is const.

Share:
12,610
cs95
Author by

cs95

Hi Magi the pirate My pandas canonicals ⬅ please critique! and/or buy me a drink ;-) When asking a question here, please describe the problem you are trying to solve, rather than asking about the method you think is the solution to that problem. This is known as the XY problem and can easily be averted if you ask the right questions. This will make it easy for us to understand what you're trying to do, and help you arrive at the best solution for your problem even sooner. Rep = participation Badges = quality 1 like = 1 prayer 1 upvote = 1 thanks Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems. (Actively Maintained) Pandas Canonicals The full list is on GitHub. Pandas Merging 101: Everything you ever wanted to know about merging and more How to iterate over a DataFrame. Spoler alert: don't! How to convert a DataFrame to NumPy array: from 0.24, use df.to_numpy() The Right Way to create an empty DataFrame and fill it: Use a list to grow your data, not a DataFrame Don't use inplace=True! Other Posts I'm Proud Of Pandas Merging 101 (see above) Best way to interleave two lists How do "and" and "or" act with non-boolean values? So you think you know recursion? In addition to this, some of my answers that use perfplot are worth a read. 236th awardee of the Legendary badge. Thanks, Stack Overflow! I love the shirt :-) & [SPACE RESERVED FOR 250k SWAG]

Updated on June 04, 2022

Comments

  • cs95
    cs95 about 2 years

    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.

    Q: Is it possible to effectively represent such a pattern in C? If yes, how can it be done?