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
Write a program to encrypt the data in a way that inputs a four digit number and replace each digit by (the sum of that digit plus 7) modulus 10. Then sweep the first digit with the third, second digit with the fourth and print the encrypted number.
What parameter does the constructor to an ofstream object take?
What is the difference between cin.read() and cin.getline()?
If dog is a friend of boy, is boy a friend of dog?
Is c++ still in demand?
Is c++ pass by reference or value?
Explain how overloading takes place in c++?
Explain storage qualifiers in c++.
What is the extension of c++?
What operator is used to access a struct through a pointer a) >> b) -> c) *
How can you create a virtual copy constructor?
What is a null object in c++?
What are friend classes? What are advantages of using friend classes?
What is name hiding in c++?
What is the difference between prefix and postfix versions of operator++()?