this might be silly!!! but i am eager to know about it:)
why are reference variables not present/used in C? why are they designed for C++?
this might be silly!!! but i am eager to know about it:)
why are reference variables not present/used in C? why are they designed for C++?
Because C was invented first. I don't know if they hadn't thought about references at the time (being mostly unnecessary), or if there was some particular reason not to include them (perhaps compiler complexity). They're certainly much more useful for object-oriented and generic constructs than the procedural style of C.
They're not present in C because they're not required. C has very few 'extraneous' features. You can write any program without using references, so they're just not included. C++ was developed much later than C was, so its designers threw in all kinds of stuff that wasn't originally present in C.
I guess C was born with a minimalist hat, and references are just syntactic sugar for pointers.
Reference arguments were originally invented, AFAIK, for one thing: operator overloading semantics. For example, operator[] just must return a reference.
It was then a subject of great debate whether the 'concealed pointer' should be used for anything else ever.
Many development convention documents of many firms say "never use references. If you need a pointer, say so".
However, it was then discovered that references have one major advantage (no, not the syntax sugar). It is this: reference is guaranteed to be valid, unless you work really hard to break it.
Personally, I still don't understand why I cannot do this in C++:
int a1, a2;
int &b = a1;
&b = a2; // Error. address of referenced is not an lvalue. Why?!
As you may know, C predates C++ by approximately a decade. References were a feature introduced in the C++ language. Some features of the C++ language have been adopted by subsequent versions of the C standard (such as const and // comment). The concept of references has not been so far.
One can hypothesize that their usefulness in object oriented programming does not translate as usefully to the procedural programming of C.
I think I agree with Pavel's idea that they were invented to make overloaded operators work properly. Its pretty clear that the first versions of C++ (C with classes) did not have references as if they did, this
would be a reference instead of a pointer.