5,431 views
2 votes
2 votes

One of the disadvantage of pass by reference is that the called function may inadvertently corrupt the caller's data. This can be avoided by :

a) declaring the actual parameters constant

b) declaring the formal parameters constant

c) passing pointers

d) None of these

3 Answers

Best answer
3 votes
3 votes

"Pass by reference is not in C"

Anyway the answer here should be b - declaring the formal parameters as a constant which then prohibits any change to the contents of the actual parameters. We can also declare the actual parameters constant -- which in turn would require formal parameters to be constant -- but this is overly restrictive as this would not allow any modification in the caller function also. 

selected by
1 votes
1 votes

truly speaking ,

Modifying a const qualified object through a pointer invokes undefined behaviour.

i.e if we want to modify a variable (declared as constant) through a pointer then result is undefined(because it depends on machine)hence we can not provide protection anymore.

therefore correct answer should be " NONE OF THESE"

0 votes
0 votes
Let us consider each option one by one...

A. If you are declaring actual parameters as constant then value of those parameters will never be change ..or u can say ,at the address of actual parameter value will remain constant.(  this is the answer of ur question) and also if ur declaring actual parameter as constant then what is there in using call by referance  if we cannot reflect the changes back??

B. If u are declaring formal parameter ( pointer to actual parameter ) as constant then u cannot change the address that is stored in that formal parameter but still u can manipulate the value present at that address...i.e the value of actual parameter ..

C. If u will pass a pointer still u can manipulate caller's function data..

Related questions

3 votes
3 votes
1 answer
2
Storm_907 asked Apr 16, 2023
461 views
Please explain this question void main() { int a =300; char *ptr = (char*) &a ; ptr++; *ptr=2; printf("%d" , a); }
4 votes
4 votes
4 answers
4
Manoj Kumar Pandey asked May 22, 2019
818 views
https://gateoverflow.in/?qa=blob&qa_blobid=14433986388826671915int main() { int a = 10; int *b = &a; scanf("%d",b); printf("%d",a+50); }What will be the Output of the fol...