How to distinguish parameter passing techniques?
Related Blog Items
- Optimizing C and C++ Code
- Stdarg: Variable Arguments
- const correctness
- Calling conventions on the x86 platform
- L-Value and R-Value Expressions
Before we see what the output of the program shown below 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.
#include
int main ( void )
{
char *a = "abc";
void f ( char * );
f ( a );
puts ( a );
}
void f ( char *a )
{
a++;
}
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.
Popularity: 5%
You need to log on to convert this article into PDF
Related Blog Items - Optimizing C and C++ Code
- Stdarg: Variable Arguments
- const correctness
- Calling conventions on the x86 platform
- L-Value and R-Value Expressions
Related Blog Items
- Optimizing C and C++ Code
- Stdarg: Variable Arguments
- const correctness
- Calling conventions on the x86 platform
- L-Value and R-Value Expressions
No Comments
No comments yet.