3.6 What is the output of following:
#include <stdio.h>
int
main ( void )
{
char *a = "abc";
void f ( char * );
f ( a );
puts ( a );
}
void
f ( char *a )
{
a++;
}
Ans Before we see what the output is, let us learn how to distinguish
two parameter passing techniques: pass by value and pass by address.
Keep the following rule in mind:
If the declaration of argument matches the declaration of the
formal parameter, then the argument is passed by value. To be more
specific, C provides only call-by-value parameter passing.
In the above example, the declarations of argument "a", and the
formal parameter "a", both are same. That means the variable "a" has
been passed by value! But, "a" stands for the address the string, and
hence the string has been passed by address. Naturally, only the
modification to the object pointed by the pointer (not in this case)
has reflection in the main (), but any modification of "a" has no
effect. Hence the output of this program is: abc.