|
| Total Answers and Comments: 3 |
Last Update: June 08, 2009 Asked by: janhavibend |
|
| | |
|
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 | Go To Top
|