- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
What is call by reference in C language?
Pass by reference means addresses are sent as arguments.
The call by reference or pass by reference method passes the arguments to a function by the means of address to an argument. This is done into the formal parameter. Inside the function, the address is used to access an actual argument.
Example
#include<stdio.h> void main(){ void swap(int *,int *); int a,b; printf("enter 2 numbers"); scanf("%d%d",&a,&b); printf("Before swapping a=%d b=%d",a,b); swap(&a, &b); //address are sent as an argument printf("after swapping a=%d, b=%d",a,b); getch(); } void swap(int *a,int *b){ int t; t=*a; *a=*b; // *a = (*a + *b) – (*b = * a); *b=t; }
Output
When the above program is executed, it produces the following result −
enter 2 numbers 10 20 Before swapping a=10 b=20 After swapping a=20 b=10
Advertisements