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
What is c++ hiding?
A company pays its salespeople on a commission basis. The salespeople receive $200 per week plus 9 percent of their gross sales for that week. For example, a saleperson who sells $5000 worth of merchandise in a week receives $200 plus 9 percent of $5000, or a total of $650. You have been supplied with a list of items sold by each salesperson. The values of these items are as follows: Item Value A 239.99 B 129.75 C 99.95 D 350.89 Write a program that inputs one salesperson's items sold in a week (how many of item A? of item B? etc.) and calculates and displays that salesperson's earnings for that week.
Where do I find the current c or c++ standard documents?
What is ios :: in in c++?
Explain the concept of friend function in c++?
What are the important differences between c++ and java?
Why is c++ a mid-level programming language?
Is c++ faster than c?
Which programming language's unsatisfactory performance led to the discovery of c++?
Where can I run c++ program?
write a corrected statement in c++ so that the statement will work properly. x = y = z + 3a;
State the difference between pre and post increment/decrement operations.
What is the use of setfill in c++?
What is a pointer with example?
What are proxy objects in c++?