GeekInterview.com
  I am new, Sign me up!
 
GeekInterview.com  >  Interview Questions  >  Programming  >  C
Go To First  |  Previous Question  |  Next Question 
 C  |  Question 425 of 453    Print  
Difference between reference and pointer?
what is difference between reference and pointer or pass by reference and pass by pointer?


  
Total Answers and Comments: 3 Last Update: June 08, 2009     Asked by: janhavibend 
  
 Sponsored Links

 
 Best Rated Answer
Submitted by: rocky2583
 
In C there is no concept of a reference variable.
Only pointers exist. When you pass address of any variable to a function, it is termed as pass by reference. Coz the formal parameter in function is the pointer that stores this address.

In C++, reference variables exist. A reference variable does not get any memory of its own. It must be initailzed when it is declared and it acts as an alias or synonym for the variable which was used to initialze it.

There are two ways to pass the variable by reference in C++

1) Traditional pass by reference approach.
someFunction (&a, &b);

void someFunction (int *x, int *y)
{
}


2)
someFunction (a, b);

void someFunction (int &x, int &y)
{
}

In this case no memory is allocated to x & y.
They act as alias for the memory locations to which a & b are referring.
any changes made to the values of x & y will cause values of a & b in the calling function to change respectively.

Both approaches 1) and 2) are called pass by reference. But first approach uses pointers and second approach uses reference variables.


Above answer was rated as good by the following members:
rajani_vaddepalli15
July 20, 2008 01:05:06   #1  
rocky2583 Member Since: July 2008   Contribution: 3    

RE: Difference between reference and pointer?
In C there is no concept of a reference variable.
Only pointers exist. When you pass address of any variable to a function it is termed as pass by reference. Coz the formal parameter in function is the pointer that stores this address.

In C++ reference variables exist. A reference variable does not get any memory of its own. It must be initailzed when it is declared and it acts as an alias or synonym for the variable which was used to initialze it.

There are two ways to pass the variable by reference in C++

1) Traditional pass by reference approach.
someFunction (&a &b);

void someFunction (int *x int *y)
{
}


2)
someFunction (a b);

void someFunction (int &x int &y)
{
}

In this case no memory is allocated to x & y.
They act as alias for the memory locations to which a & b are referring.
any changes made to the values of x & y will cause values of a & b in the calling function to change respectively.

Both approaches 1) and 2) are called pass by reference. But first approach uses pointers and second approach uses reference variables.

 
Is this answer useful? Yes | NoAnswer is useful 1   Answer is not useful 0Overall Rating: +1    
September 28, 2008 07:15:22   #2  
prkgroup Member Since: September 2008   Contribution: 1    

RE: Difference between reference and pointer?
Reference is any variable address represent '&' with front. But pointer is a variable. Which reference another variable. Reference variable holds value. Which is not a address.
 
Is this answer useful? Yes | No
June 08, 2009 09:13:13   #3  
mail2kul Member Since: June 2009   Contribution: 1    

RE: Difference between reference and pointer?

C++ supports passing an object by reference and pointer former is widely used in the C++ where as C doesnot support passing user defined data by reference.

Consider the following example

Employee *ptr; // is a placeholder
Employee &ref; // compiler throws error

Pointer may point to variable or not at any given point in time. We can define pointer without having any data in hand. later we can make it to point to some data of defined type.
Reference can't be defined as pointer. to define reference we need to have data already available failing to do so generates compiler errror.

Above statments can be made working as
Employee &ref manger; // manager is already created object.



 
Is this answer useful? Yes | No


 
Go To Top


 Sponsored Links

 
About Us -  Privacy Policy -  Terms and Conditions -  Contact -  Ask Question -  Propose Category -  Site Updates 

Copyright © 2005 - 2009 GeekInterview.com. All Rights Reserved

Page copy protected against web site content infringement by Copyscape