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 a storage class?
What is endl c++?
How the memory management in vectors are being done. What happens when the heap memory is full, and how do you handle it ?
What function initalizes variables in a class: a) Destructor b) Constitutor c) Constructor
What is the use of structure in c++?
What is the main use of c++?
How compile and run c++ program in turbo c++?
What is the use of lambda in c++?
Is it possible to pass an object of the same class in place of object reference to the copy constructor?
What is the difference between global variables and static varables?
How a modifier is similar to mutator?
how to access grid view row?
How can a called function determine the number of arguments that have been passed to it?
What are the advantages of prototyping?
What is the difference between object-oriented programming and procedural programming?