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

What are iterators in c++?

587


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].

949


How much do c++ programmers make?

556


How would you implement a substr() function that extracts a sub string from a given string?

557


What is #include cstdlib in c++?

647






Can we use struct in c++?

583


What are the differences between malloc() and calloc()?

607


What is class and structure in c++?

551


Is c++ platform dependent?

627


What is the use of "new" operator?

652


How do you declare A pointer to function which receives an int pointer and returns a float pointer

670


What is c++ virtual inheritance?

590


What are 2 ways of exporting a function from a dll?

605


Why c++ is not a pure oop language?

549


Define a pdb file.

635