Explain "passing by value", "passing by pointer" and
"passing by reference" ?
Answer Posted / ven
Pass by value - a copy is made
Pass by pointer ( explicit pointer)
example:
void func(int * ptr_sent)
main()
{
int i;
int *p;
p = &i;
func(p);
}
void func(int * ptr_sent)
{
*ptr_sent = *ptr_sent + 2
// adds 2 to the value in location pointed by ptr_sent
}
Pass by reference (implicit pointer)
example:
void func(int &ref_sent)
main()
{
int i;
func(&i);
}
void func(int &ref_sent)
{
ref_sent = ref_sent + 2
// adds 2 to the ref_sent
// Please note that you do not need * when using reference
// Any code manipulating reference reflects changes on i
}
| Is This Answer Correct ? | 2 Yes | 0 No |
Post New Answer View All Answers
What are register variables?
What is static class data?
What are the different types of comments allowed in c++?
How to give an alternate name to a namespace?
Can you explicitly call a destructor on a local variable?
What is math h in c++?
What is the use of data hiding?
What is a map in c++?
Explain the difference between static and dynamic binding of functions?
what are the iterator and generic algorithms.
What is a modifier in c++?
Is there a c++ certification?
What is the benefit of c++?
Give the difference between the type casting and automatic type conversion. Also tell a suitable C++ code to illustrate both.
What is c++ best used for?