Function returning Reference to a structure
Answers
Answered by
0
Hello Dearie !
_____________________________________________________________
In C++ , A function can return a reference to a structure . For example , consider the following code :
_____________________________________________________________
Struct time
{
int hours ;
int minutes ;
int seconds;
} result ; //global structure variable declared
void main ( )
{
time and convert(long) ; //notice the function prototype
long sec ;
-------------------------------------
-------------------------------------
result = convert(sec) ; //function call
-------------------------------------
-------------------------------------
}
//function definition convert ( )
time & converting (long sec)
{
long x;
result.seconds = sec%60;
x = sec/60
result.minutes = x%60;
result.hours - x/60;
return result;
}
here the function convert ( ) is returning reference to a structure result . when a function returns a refrence , it returns the lvalue (location value ) in place of rvalue (data value ) of a variable
_____________________________________________________________
In C++ , A function can return a reference to a structure . For example , consider the following code :
_____________________________________________________________
Struct time
{
int hours ;
int minutes ;
int seconds;
} result ; //global structure variable declared
void main ( )
{
time and convert(long) ; //notice the function prototype
long sec ;
-------------------------------------
-------------------------------------
result = convert(sec) ; //function call
-------------------------------------
-------------------------------------
}
//function definition convert ( )
time & converting (long sec)
{
long x;
result.seconds = sec%60;
x = sec/60
result.minutes = x%60;
result.hours - x/60;
return result;
}
here the function convert ( ) is returning reference to a structure result . when a function returns a refrence , it returns the lvalue (location value ) in place of rvalue (data value ) of a variable
Similar questions