Home >Backend Development >C++ >Why Can't C References Be Reset?
Why References in C are not Resettable
C references, unlike pointers, adhere to two fundamental characteristics: they must always reference the same object and cannot be null. While pointers offer flexibility in pointing to different objects and can be set to null, C lacks a concept of "non-nullable, reseatable references or pointers." This raises the question of why references cannot be reset.
Felix Nievelstein initially raised this question, emphasizing the use of references to ensure that associations between objects remain valid. However, according to Bjarne Stroustrup, the designer of C , the primary reason for the immutability of references is to avoid potential ambiguities and errors.
In Stroustrup's words, "I had in the past been bitten by Algol68 references where r1=r2 can either assign through r1 to the object referred to or assign a new reference value to r1 (re-binding r1) depending on the type of r2. I wanted to avoid such problems in C ."
By prohibiting reference rebinding, C aims to eliminate uncertainty and the risk of inadvertently altering the reference's destination object. This design decision ensures that references consistently point to the same object, preventing unexpected consequences and maintaining program correctness.
The above is the detailed content of Why Can't C References Be Reset?. For more information, please follow other related articles on the PHP Chinese website!