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 are iterators in c++?
Suppose that data is an array of 1000 integers. Write a single function call that will sort the 100 elements data [222] through data [321].
How much do c++ programmers make?
How would you implement a substr() function that extracts a sub string from a given string?
What is #include cstdlib in c++?
Can we use struct in c++?
What are the differences between malloc() and calloc()?
What is class and structure in c++?
Is c++ platform dependent?
What is the use of "new" operator?
How do you declare A pointer to function which receives an int pointer and returns a float pointer
What is c++ virtual inheritance?
What are 2 ways of exporting a function from a dll?
Why c++ is not a pure oop language?
Define a pdb file.