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
Are strings mutable in c++?
Is main a class in c++?
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.
What is the difference between a "copy constructor" and an "assignment operator" in C++?
What is null pointer and void pointer and what is their use?
What is :: operator in c++?
Is java a c++?
What is the best c++ ide?
How compile and run c++ program in turbo c++?
What is a local variable?
What is constant in c++ with example?
Define whitespace in C++.
Differentiate between late binding and early binding.
what you know about c++?
State two differences between C and C++.