You are on page 1of 6

NETWORKING PROGRAMMING DBMS OPERATING SYSTEM INTERNET

HARDWARE SOFTWARE

Di erence Between Pointer and Reference


May 18, 2016 — Leave a Comment

The “pointer” and “reference” both are used to point or refer an another variable. But, the basic
difference among both of them is that a pointer variable points to a variable whose memory
location is stored in it. The reference variable is an alias for a variable which is assigned to it.
The comparison chart below explores the other differences between a pointer and a
reference.
Content: Pointer Vs Reference
1. Comparison Chart
2. Definition
3. Key Differences
4. Conclusion

Comparison Chart

BASIS FOR
POINTER REFERENCE
COMPARISON

Basic The pointer is the memory address of a The reference is an alias for a
variable. variable.

Returns The pointer variable returns the value The reference variable returns
located at the address stored in pointer the address of the variable
variable which is preceded by the pointer preceded by the reference
sign '*'. sign '&'.

Operators *, -> &

Null Reference The pointer variable can refer to NULL. The reference variable can
never refer to NULL.

Initialization An uninitialized pointer can be created. An uninitialized reference can


never be created.
BASIS FOR
POINTER REFERENCE
COMPARISON

Time of The pointer variable can be initialized at The reference variable can
Initialization any point of time in the program. only be initialized at the time
of its creation.

Reinitialization The pointer variable can be reinitialized The reference variable can
as many times as required. never be reinitialized again in
the program.

Kalyan Jewellers
Kalyan Jewellers

Win Free assured Gold coins at your


nearest Kalyan store

NEW DELHI

WEBSITE DIRECTIONS

De nition of Pointer
A “pointer” is a variable that holds the memory location of another variable. The operators
used by the pointer variable is * and ->. The declaration of pointer variable contains the base
data type followed by the ‘*’ sign and the variable name.

1. type *var_name;

Let us understand pointer with the help of an example.

1. int a=4;
2. int * ptr= &a;
3. cout<<a; //4
4. cout<<ptr; //2007 address of varaible a
5. cout<<*ptr; //4
Here, we have an integer variable a and, a pointer variable ptr which stores the address of
variable a.

Pointer Arithmetic
The pointer variable can be operated with two arithmetic operators that are “addition” and
“subtraction”. The addition referred as “increment”, and the subtraction is referred as
“decrement”. As a pointer variable is incremented, it points to the memory location of the next
variable of its base type. As a pointer variable is decremented, it points to the memory
location of the previous variable of its base type. Hence, an array can be efficiently accessed
by a pointer variable.

Multiple Indirection
A pointer points to the other pointer variable which is pointing to the target value. This kind of
pointer is always initialized with the address of another pointer variable. The declaration of a
pointer to a pointer is as follow.

1. type **var_name;

Let’s study it with an example.

1. int a=4;
2. int * ptr1= &a;
3. int ** ptr2= &ptr1;
4. cout<<a; //4
5. cout<<ptr1; //2007 address of varaible a
6. cout<<*ptr1; //4
7. cout<<ptr2; //1007 address of pointer variable ptr1.
8. cout<<**ptr2; //4

Function pointer
As we know that a function is not a variable, still it has a memory location, that can be
assigned to a pointer variable. Once a pointer points to a function, then the function can be
called with that function pointer.

The important points to remember about the pointer.

The pointer variable can be created without its initialization, and it can be initialized
anywhere in the program.
The pointer variable can be reinitialized to an another variable.
The pointer variable can refer to NULL.

De nition of Reference
The reference variable is used to refers to the variable which is assigned to that reference
variable. The operator used by the reference variable is ‘&’. The declaration of a reference
variable contains base type followed by ‘&’ sign and then variable name.

1. type & refer_var_name = var_ name;

Here, the type is the datatype, the & operator confirms that it is a reference variable. The
refer_var_name is the name of the reference variable. The var_name is the name of the
variable, which we want the reference variable to refer.

Let us understand the reference variable with the help of an example.

1. int a = 4;
2. int &b = a; // b refers to a
3. b = 6; // now a = 6

Here, the variable of type int is assigned a value 4. The reference variable is assigned the
variable a, i.e. b is alias of a. Now, when we assign another value to b, we modify the value of
a. Hence, it can be said that the changes done to a reference variable will also occur in
variable referred by that reference variable.

The most important point is that the reference variable must be initialized at the time of its
creation.Once the reference variable is initialized with a variable, it can not be reinitialized to
refer an another variable. The moment you assign a value to a reference variable, you assign
that value to a variable that a reference variable points to. The reference variable can never
refer to  NULL. Arithmetic can not be performed on a reference variable.

The reference variable can be used in three ways:

As a function return value.


As a function parameter.
As a stand alone reference.

Key Di erences Between Pointer and Reference


1. Reference is  like creating an another name to refer a variable so that it can be
refered with different names. On the other hand, a pointer is simply a memory
address of a variable.

2. A pointer variable if preceeded by ‘*’ returns the value of a variable whose


address is stored in the pointer varaible. A reference variable when preceeded
by ‘&’ returns the address of that variable.

3. Pointer operators are * and -> whereas, reference operator is &.

4. A pointer variable if does not carry any variable’s address it points to null. On the
other hand, a reference variable can never refer to Null.

5. You can always create an unitialized pointer variable, but we create a reference
when we need an alias of some variable so you can never create an unitialize
refernce.

6. You can reinitialize a pointer but once you initialize arefernce you can not
reinitialize it again.

7. You can create an empty pointer and initialize it at any time but you have to
initialize refrence only when you create a refernce.

Note:

Java does not support pointers.

Conclusion
The pointer and reference both are used to point or refer another variable. But both differ in
their usage and implementation.

Samsung Guru 1200 (GT-E1200 …


Rs. 1,100.00
(details + delivery)

You might also like