Explain "passing by value", "passing by pointer" and
"passing by reference" ?
Answer Posted / ranjeet garodia
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
}
answers given by yen .. there is one error ... in pass by
reference .. when calling function pass the variable not
the address....fun(i) should be called instead of fun(&i)
Pass by reference (implicit pointer)
example:
void func(int &ref_sent)
main()
{
int i;
func(i); // call by reference
}
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 ? | 1 Yes | 1 No |
Post New Answer View All Answers
Does there exist any other function which can be used to convert an integer or a float to a string?
How a macro differs from a template?
What is implicit conversion/coercion in c++?
Do class method definitions?
What is fixed in c++?
Which is most difficult programming language?
If you push the numbers (in order) 1, 3, and 5 onto a stack, which pops out first a) 1 b) 5 c) 3
What are the new features that iso/ansi c++ has added to original c++ specifications?
What is singleton pattern in c++?
what kind of projects are suitable for c and c++
Explain what you mean by a pointer.
what is C++ objects?
Do you know what are static and dynamic type checking?
What is flush () in c++?
What are the classes in c++?