search
Login

Categories


C Books Guide and List
C++ Books Guide and List
Best Java Books

3.8k questions

4k answers

117 comments

10.7k users

0 votes
1.4k views
asked Expert 13.2k points 26 40 57

1 Answer

0 votes
Pass by Reference: In this method, the addresses of actual arguments in the calling function are copied

into formal arguments of the called function. This means that using these addresses, we would have an access to the actual arguments and hence we would be able to manipulate them. C does not support Call by reference. But it can be simulated using pointers.

Example:

#include <stdio.h>

/* function definition */

void swap(int *x, int *y) {

int t;

t = *x; /* assign the value at address x to t */

*x = *y; /* put the value at y into x */

*y = t; /* put the value at to y */

} int main() {

int m = 10, n = 20;

printf("Before executing swap m=%d n=%d\n", m, n);

swap(&m, &n);

printf("After executing swap m=%d n=%d\n", m, n);

return 0;

}

Output:

Before executing swap m=10 n=20

After executing swap m=20 n=10

Explanation:

In the main function, address of variables m, n are sent as arguments to the function 'swap'. As swap

function has the access to address of the arguments, manipulation of passed arguments inside swap function would  be directly reflected in the values of m, n.
answered Guru 39.1k points 10 21 32

Related questions

0 votes
1 answer 1.4K views
1.4K views
asked keem Expert 13.2k points 26 40 57
0 votes
1 answer 42 views
0 votes
1 answer 3.1K views
1 vote
3 answers 3.9K views
3.9K views
what is mean by pass by value and pass by reference in C code?
asked anonymous
0 votes
1 answer 4.4K views
...