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
Is c++ the hardest language?
What is c++ and its features?
Write about an iterator class?
write a function signature with various number of parameters.
Inline parameters : What does the compiler do with the parameters of inline function, that can be evaluated in runtime ?
What are the basic data types used in c++?
What is vector pair in c++?
Why is main function important?
Does dev c++ support c++ 11?
Will a catch statement catch a derived exception if it is looking for the base class?
Which is better c++ or java?
Can you help me with this one? Make a program that when a user inputed a Product Name, it will display its price, and when the user inputed the quantity of the inputed product, it will show its total price. The output must be like this: Product Name: Price: Quantity: Total Price: ..this is the list of products to be inputed: Cellphone - 1500 Washing Machine - 5200 Television - 6000 Refrigirator - 8000 Oven - 2000 Computer - 11000 thanks..:D
Can I learn c++ without learning c?
Explain static and dynamic memory allocation with an example each.
What is the use of object in c++?