(C Programming Tutorials)
C Call by Reference: Using pointers
In this tutorial, you’ll learn to pass addresses as arguments to the functions with the help of examples. This technique is known as call by reference.
To accept these addresses in the function definition, we can use pointers. It’s because pointers are used to store addresses. Let’s take an example:
Example: Call by reference
#include <stdio.h>
void swap(int *n1, int *n2);
int main(){
int num1 = 5, num2 = 10;
// address of num1 and num2 is passed
swap( &num1, &num2);
printf("num1 = %dn", num1);
printf("num2 = %d", num2);
return 0;
}
void swap(int* n1, int* n2){
int temp;
temp = *n1;
*n1 = *n2;
*n2 = temp;
}
When you run the program, the output will be:
num1 = 10 num2 = 5
The address of num1 and num2 are passed to the swap()
function using swap(&num1, &num2);
.
Pointers n1 and n2 accept these arguments in the function definition.
void swap(int* n1, int* n2){
... ..
}
When *n1 and *n2 are changed inside the swap()
function, num1 and num2 inside the main() function are also changed.
Inside the swap()
function, *n1
and *n2
swapped. Hence, num1 and num2 are also swapped.
Notice that, swap()
is not returning anything; its return type is void
.
This technique is known as call by reference in C programming.
Example 2: Passing Pointers to Functions
#include <stdio.h>
void addOne(int* ptr){
(*ptr)++; // adding 1 to *ptr
}
int main(){
int* p, i = 10;
p = &i;
addOne(p);
printf("%d", *p); // 11
return 0;
}
Here, the value stored at p, *p
, is 10 initially.
We then passed the pointer p to the addOne()
function. The ptr pointer gets this address in the addOne()
function.
Inside the function, we increased the value stored at ptr by 1 using (*ptr)++;
. Since ptr and p pointers both have the same address, *p
inside main()
is also 11.
Disclaimer: The information and code presented within this recipe/tutorial is only for educational and coaching purposes for beginners and developers. Anyone can practice and apply the recipe/tutorial presented here, but the reader is taking full responsibility for his/her actions. The author (content curator) of this recipe (code / program) has made every effort to ensure the accuracy of the information was correct at time of publication. The author (content curator) does not assume and hereby disclaims any liability to any party for any loss, damage, or disruption caused by errors or omissions, whether such errors or omissions result from accident, negligence, or any other cause. The information presented here could also be found in public knowledge domains.
Learn by Coding: v-Tutorials on Applied Machine Learning and Data Science for Beginners
Latest end-to-end Learn by Coding Projects (Jupyter Notebooks) in Python and R:
All Notebooks in One Bundle: Data Science Recipes and Examples in Python & R.
End-to-End Python Machine Learning Recipes & Examples.
End-to-End R Machine Learning Recipes & Examples.
Applied Statistics with R for Beginners and Business Professionals
Data Science and Machine Learning Projects in Python: Tabular Data Analytics
Data Science and Machine Learning Projects in R: Tabular Data Analytics
Python Machine Learning & Data Science Recipes: Learn by Coding
R Machine Learning & Data Science Recipes: Learn by Coding
Comparing Different Machine Learning Algorithms in Python for Classification (FREE)
There are 2000+ End-to-End Python & R Notebooks are available to build Professional Portfolio as a Data Scientist and/or Machine Learning Specialist. All Notebooks are only $29.95. We would like to request you to have a look at the website for FREE the end-to-end notebooks, and then decide whether you would like to purchase or not.