9

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?

6
  • 1
    possible duplicate of How is a Java reference different from a C pointer? and of What's a nice explanation for pointers?
    – gnat
    Commented Apr 18, 2013 at 7:25
  • 2
    @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.
    – yfklon
    Commented Apr 18, 2013 at 7:28
  • 3
    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). Commented Apr 18, 2013 at 7:28
  • 5
    Pointers and references are nothing to do with OOP vs Procedural.
    – mattnz
    Commented Apr 18, 2013 at 7:43
  • 1
    @blank this can not be answered in general as the answer is different for different langugaes
    – jk.
    Commented Apr 18, 2013 at 8:24

5 Answers 5

21

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.

5
  • 2
    "or using some other mechanism" An Example would be nice. Commented Apr 18, 2013 at 9:51
  • 1
    @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
    Commented Apr 18, 2013 at 10:04
  • 2
    @Angelo.Hannes, I updated it with an example.
    – user82096
    Commented Apr 18, 2013 at 10:21
  • @dan1111 pointer is not always a variable for example 'this' is also a pointer but not a variable Commented Aug 26, 2013 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 :)
    – user82096
    Commented Sep 4, 2013 at 8:08
6

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).

4

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->setColor(red);

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

2

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.

5
  • I want to know in OOP lanuages, are references have additional properties, or are they just stores an address like pointers?
    – yfklon
    Commented Apr 18, 2013 at 7:36
  • 1
    @blank: Under the bonnet (assuming a C++ style differentiation) they are the same. However the syntax differences make the difference in use significant.
    – mattnz
    Commented Apr 18, 2013 at 7:49
  • 1
    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
    Commented Apr 18, 2013 at 8:40
  • 2
    @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. Commented Apr 18, 2013 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
    Commented Apr 18, 2013 at 9:15
1

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/

2
  • 5
    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. Commented Apr 18, 2013 at 7:31
  • Yep @Joachim Sauer is right its c++ specific
    – Grey
    Commented Apr 18, 2013 at 8:57

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