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


Please Help Members By Posting Answers For Below Questions

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.

2299


What parameter does the constructor to an ofstream object take?

838


What is the difference between cin.read() and cin.getline()?

827


If dog is a friend of boy, is boy a friend of dog?

784


Is c++ still in demand?

881


Is c++ pass by reference or value?

799


Explain how overloading takes place in c++?

773


Explain storage qualifiers in c++.

825


What is the extension of c++?

715


What operator is used to access a struct through a pointer a) >> b) -> c) *

865


How can you create a virtual copy constructor?

813


What is a null object in c++?

853


What are friend classes? What are advantages of using friend classes?

871


What is name hiding in c++?

888


What is the difference between prefix and postfix versions of operator++()?

830