Home >Backend Development >C++ >Why Are C References Immutable?
Why References in C Are Immutable
C references possess two defining characteristics: they permanently bind to an object and cannot be null. In contrast, pointers offer flexibility by enabling access to various objects and supporting null values. While this distinction may seem arbitrary, the immutability of references in C stems from well-founded design principles.
According to Bjarne Stroustrup, the primary architect of C , the inability to modify references fosters stability and prevents complexities that can arise with rebinding. In Algol68, references could be reassigned to either target a specific object or a different reference value. This fluidity, however, created confusion and introduced potential ambiguities.
By eliminating the ability to reassign references, C ensures uniformity and eliminates the chances of accidental reference changes. This rigidity guarantees that each reference consistently points to the same object, ensuring the integrity and coherence of the program's data structure.
The above is the detailed content of Why Are C References Immutable?. For more information, please follow other related articles on the PHP Chinese website!