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 is rtti in c++?
Explain polymorphism?
Out of fgets() and gets() which function is safe to use?
How do you invoke a base member function from a derived class in which you have not overridden that function?
Define basic type of variable used for a different condition in C++?
Is c++ an oop?
Does improper inheritance have a potential to wreck a project?
What are structs in c++?
what is data encapsulation in C++?
Explain dangling pointer.
How to tokenize a string in c++?
How is computer programming useful in real life?
What is microsoft c++ redistributable?
Which c++ operator cannot overload?
Define pointers?