tags:

views:

152

answers:

6

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++?

+6  A: 

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.

Cogwheel - Matthew Orlando
+1  A: 

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.

Carl Norum
A: 

I guess C was born with a minimalist hat, and references are just syntactic sugar for pointers.

Mau
That's a very simplistic view of references and though technically (at a very low level) true not actually an accurate description of what references are.
Martin York
+3  A: 

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?!
Pavel Radzivilovsky
That code makes no sense. The second line tries to initialise an integer from a pointer; the third tries to assign an integer to a temporary pointer. Both of these give a type mismatch. The error tells you that you can't assign to a temporary; even if you could, then it would have no effect since the temporary is immediately destroyed. What is it that you want to do?
Mike Seymour
bta
sorry me idiot. fixed code. please disregard whatever you think I meant.
Pavel Radzivilovsky
David Thornley
Operator[] can easily return a pointer. I believe it was more the parameters to operator+, for instance, that made references neccessary.
Dennis Zickefoose
Pavel Radzivilovsky
+1  A: 

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.

Amardeep
+1  A: 

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.

Chris Dodd