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

Are strings mutable in c++?

915


Is main a class in c++?

779


There are 100 students in a class. The management keep information in two tables. Those two tables are given like Roll no Name Age 001 ABC 15 002 XYZ 14 and Roll No Subject Marks 001 Math 75 001 Physics 55 002 Math 68 001 Hindi 69 They want the information like this Roll No Name Hindi Physics Math Total 001 ABC 69 55 75 199 002 XYZ 68 74 84 226 And Roll No Suject Highest 001 Math 98 007 Physics 84 021 Hindi 74 All 275 All information is kept in structure in main memory. You have to find last two tables.

2779


What is the difference between a "copy constructor" and an "assignment operator" in C++?

866


What is null pointer and void pointer and what is their use?

849


What is :: operator in c++?

820


Is java a c++?

797


What is the best c++ ide?

864


How compile and run c++ program in turbo c++?

888


What is a local variable?

872


What is constant in c++ with example?

890


Define whitespace in C++.

987


Differentiate between late binding and early binding.

1004


what you know about c++?

895


State two differences between C and C++.

841