Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

This question already has an answer here:

References and pointers do the same thing as I know. Is there any difference between them?

If there is no difference, why we call them reference not pointer?

share|improve this question

marked as duplicate by gnat, Joachim Sauer, Joris Timmermans, jk., Bart van Ingen Schenau Apr 18 '13 at 10:27

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

@gnat Yes, its quite duplicate. I haven't seen that, but there is a little difference. I asked it in general. Not for only Java and C. –  blank Apr 18 '13 at 7:28
Note that the first question is Java-vs-C++. It's a loaded question because the use of those terms is not entirely consistent across languages (and sometimes even within a single language). –  Joachim Sauer Apr 18 '13 at 7:28
Pointers and references are nothing to do with OOP vs Procedural. –  mattnz Apr 18 '13 at 7:43
@blank this can not be answered in general as the answer is different for different langugaes –  jk. Apr 18 '13 at 8:24

5 Answers 5

up vote 15 down vote accepted

Broadly, you can make the following distinction:

A reference is a variable that refers to something else and can be used as an alias for that something else.

A pointer is a variable that stores a memory address, for the purpose of acting as an alias to what is stored at that address.

So, a pointer is a reference, but a reference is not necessarily a pointer.

Pointers are a particular implementation of the concept of a reference, and the term tends to be used only for languages that give you direct access to the memory address.

References can be implemented internally in a language using pointers, or using some other mechanism. For example, Perl has symbolic references, which refer to the variable by name rather than memory location. Which is why weird code like this works:

my $bar = "foo";
$$bar = "Am I foo or bar?"; #This is actually a reference to $foo!
print $foo;

This distinction between references and pointers is not a hard and fast rule (C++ being the main exception, because it has both references and pointers, with a distinct meaning for each) but it covers most of the usage.

share|improve this answer
"or using some other mechanism" An Example would be nice. –  Angelo.Hannes Apr 18 '13 at 9:51
@Angelo.Hannes In C++ if a reference refers to a value that the compiler knows the address of (such as a localvariable) then the compiler can just use that variable without storing the reference in a pointer and dereferencing it. –  Will Apr 18 '13 at 10:04
@Angelo.Hannes, I updated it with an example. –  dan1111 Apr 18 '13 at 10:21
@dan1111 pointer is not always a variable for example 'this' is also a pointer but not a variable –  basha Aug 26 '13 at 14:21
@sunny, I would consider this to be a special variable. But I suppose that's a matter of opinion--the definition of "variable" would be a whole other question :) –  dan1111 Sep 4 '13 at 8:08

There is no generally accepted difference between pointers and references if you look at a wide enough distribution of languages using the terms. Variant of the same concepts use both terms (and some other like access) nearly indifferently, yet some characteristics tend to be closely associated with one of the term:

  • if arithmetic is available, I've never seen the term reference used. (But pointer doesn't imply that arithmetic is available)

  • if the referenced object can't be changed, I've never seen the term pointer used excepted when there is a constant property which can be applied to the pointer as well as to other things. (But reference doesn't imply non rebindability).

share|improve this answer

Pointers and references are placeholder to use objects.

Instead of using the object itself you can use a pointer or reference to access and/or manipulate the object.

Car myCar = new Car();
Car& myCarReference = myCar;

Changing the content of myCarReference will change the content of myCar.

Pointers add powerful but potentially dangerous address operations to references. For example:

Car* myCarPointer;

    // 0x12345678=the memory adress where one car 
    // object is supposed to be in memory.
myCarPointer = 0x12345678;  

myCarPointer+=20;  // advance 20 cars in an array

Potentially dangerous means "you can assign any address to a pointer. The pointer can point to a location where there is no car object.

Languages like java and c# introduced references to make programms more robust compared to c/c++ which have only pointers

share|improve this answer

You are juxtaposing the wrong things. In C++, pointers and references have different meanings, but because you aren't asking about C++, I suspect that isn't what you are after. If you are, check out Harssh's answer.

What you probably want to know is the difference between a reference to an object and the object itself. In most newer languages you can pass an object around by value or by reference.

Passing an object by value means making a copy of it. You can modify that copy without affecting the original. Making that copy can cost a lot of memory access though.

Passing an object by reference means passing a handle to that object. This is cheaper because you don't need to make a copy. It also means that any changes you make will affect the original.

Functional programming languages will tend to pass everything by value because that avoids side effects.

share|improve this answer
I want to know in OOP lanuages, are references have additional properties, or are they just stores an address like pointers? –  blank Apr 18 '13 at 7:36
@blank: Under the bonnet (assuming a C++ style differentiation) they are the same. However the syntax differences make the difference in use significant. –  mattnz Apr 18 '13 at 7:49
I don't think functional languages pass everything by value. Side effect-free behavior comes from immutable values, not copying value on every function call. How would you efficiently implement algorithms with lists, if you had to copy them every time? –  ionoy Apr 18 '13 at 8:40
@ionoy: In a functional language, it is impossible to distinguish pass-by-value vs. pass-by-reference. Using one or the other doesn't change the meaning of the program. Compilers and interpreters take advantage of this freedom and choose whichever one fits best for a certain context. E.g. for small objects that are not significantly bigger than a reference would be, it doesn't make sense to pass them by reference. For large objects, it doesn't make sense to pass them by value. You could also pass the reference to the large object by value as a third option. It doesn't matter. –  Jörg W Mittag Apr 18 '13 at 8:55
Well, I wasn't assuming that. I was just objecting to "Functional programming languages will tend to pass everything by value" statement. –  ionoy Apr 18 '13 at 9:15

Pointers and references look different enough (pointers use the * and -> operators, references use .), but they seem to do similar things. Both pointers and references let you refer to other objects indirectly. How, then, do you decide when to use one and not the other? First, recognize that there is no such thing as a null reference. A reference must always refer to some object. As a result, if you have a variable whose purpose is to refer to another object, but it is possible that there might not be an object to refer to, you should make the variable a pointer, because then you can set it to null. On the other hand, if the variable must always refer to an object, i.e., if your design does not allow for the possibility that the variable is null, you should probably make the variable a reference.

for more refer http://www.cplusplus.com/articles/ENywvCM9/

share|improve this answer
Note that this answer is tells what pointers/references are in C++. Not every programming language uses those terms to mean the exact same thing. –  Joachim Sauer Apr 18 '13 at 7:31
Yep @Joachim Sauer is right its c++ specific –  Harssh Apr 18 '13 at 8:57

Not the answer you're looking for? Browse other questions tagged or ask your own question.