Explain "passing by value", "passing by pointer" and
"passing by reference" ?

Answers were Sorted based on User's Feedback



Explain "passing by value", "passing by pointer" and "passing by reference..

Answer / roshanpr

During pass by value a duplicate copy of the parameters
passed are created. Any changes made to copy will not
reflect the actual parameters.

In pass by pointer(it called as pass by address) duplicate
copy is not created. and any chagnes made to copy will
reflect in actual parameters also.

Is This Answer Correct ?    10 Yes 1 No

Explain "passing by value", "passing by pointer" and "passing by reference..

Answer / 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

Explain "passing by value", "passing by pointer" and "passing by reference..

Answer / ranjeet garodia

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
}

answers given by yen .. there is one error ... in pass by
reference .. when calling function pass the variable not
the address....fun(i) should be called instead of fun(&i)

Pass by reference (implicit pointer)
example:
void func(int &ref_sent)
main()
{
int i;
func(i); // call by reference
}

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 ?    1 Yes 1 No

Explain "passing by value", "passing by pointer" and "passing by reference..

Answer / sampurna pandey

i am not agree with Ranjeet, you mention this

main()
{
int i;
func(i); // call by reference
}

but fun(i) is a call by value not call by reference

your code will give compile error.

Ven eample is right.

Is This Answer Correct ?    1 Yes 1 No

Explain "passing by value", "passing by pointer" and "passing by reference..

Answer / sanish joseph

In Pass by value,copy of the value is passed, but in pass by
reference the address of the variables are passed as a
reference.pointer are variables storing the address of
another variable so it can be used as a pass by referece,
den it s teamed "passing by pointer".

Is This Answer Correct ?    3 Yes 4 No

Post New Answer

More C++ General Interview Questions

what are Access specifiers in C++ class? What are the types?

0 Answers  


What is array give example?

0 Answers  


Will a recursive function without an end condition every quit, in practice a) Compiler-Specific (Some can convert to an infinite loop) b) No c) Yes

0 Answers  


What is c++ redistributable?

0 Answers  


What is iterator c++?

0 Answers  






find the two largest values among the 6 numbers using control structures : do-while,for,if else,nestedif- else ,while. one or two of them.

0 Answers  


What return value must conversion operators have in their declaration?

0 Answers  


What are the two main components of c++?

0 Answers  


If we declare two macro with the same identifier without doing undef the first, what will be the result? eg: #define MAX_SIZE 100 #define MAX_SIZE 200 int table1[MAX_SIZE];

3 Answers  


What are c++ templates used for?

0 Answers  


If you want to share several functions or variables in several files maitaining the consistency how would you share it?

0 Answers  


What do you know about near, far and huge pointer?

0 Answers  


Categories