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
How come you find out if a linked-list is a cycle or not?
How important is c++?
How should a contructor handle a failure?
Out of fgets() and gets() which function is safe to use?
How many types of modularization are there in c++?
What are the two types of polymorphism?
What are the advantages of using typedef in a program?
How does a C++ structure differ from a C++ class?
Which sort does c++ use?
Function can be overloaded based on the parameter which is a value or a reference. Explain if the statement is true.
Explain what is oop?
What is the c++ programming language used for?
What is the outcome of cout< a) 16 b) 17 c) 16.5
Explain the difference between overloading and overriding?
Explain what are the sizes and ranges of the basic c++ data types?