In this blog, we are going to cover the pass by reference concept which is one of the most important concepts in C programming and the alternative of pass by reference is call by reference both are similar because if the method who is calling a function by some reference then they can say that call by reference and who is passing value to someone refer into that then it can be called as pass by reference.
Introduction( with a problem statement.)
In C programming, we are using the call by reference or pass by reference method to achieve modification in the passed variables i.e actual parameter from that function. In c programming variable passed to a function from the main function is not getting affected by that function we can just return some value from the called function and store it in another variable. when we passed some actual parameter to a function then the function creates its own temporary variables and after using this temporary variable function vanishes all the created variables. So to achieve some change in a variable we use call by reference with the help of pointers and pass these pointers as a parameter. We will understand the concept behind calling by reference by solving one problem statement i.e C Program To Sort The List Of Numbers Using Pass By Reference.
Array Of Numbers
The given problem statement will be holding certain distinct numbers in an array so that we can sort it accordingly. let’s see what are the ways to declare arrays in c and initialize accordingly.
1. int arr[8];// this way we can specify the maxium length of the array which all a useful method for intializing array.
arr[0] =1;
2. int arr[] = { 80, 60, 12, 72, 8, 78, 44, 39 }; // we can declare array of number in curly braces with separators comma in this case.
Pointers Concept
Pointers in C are used to hold the address of variables instead of their values and to declare pointers in c we use an asterisk (i.e *variable name). Pointers can be declared as int(int*), char, float, or function datatype in C which points to the location of that datatype example char *p it will point to character datatype only.
#include <stdio.h>
int main() {
// Write C code here
int a,b;
a = 19;
int* c, *d;
c = &a;
d = a;
printf("%d\n", c);//1234 address of a
printf("%d\n", &a);//1234 address of a
printf("%d\n", *c);//19 value at address a pointed by *c
printf("%d\n", a);//19 value of a
printf("%d", *d);//output will be segmentation fault because no such address is their which is 19 in this case that while pointing *d it gives segmentation fault error.
} //This Block Code is related to pointer concept.
Call By Reference
Call by reference is a method in which we are trying to pass pointer variables as a formal parameter so that we can point to address and do some modifications accordingly.
eg void sortingList(int n, int* pta).
Loop Through Array
To Print out the variables stored in an array or to do some modification, we can do that by looping it from the start index to the end index of the array in C.
eg:
for(int i = 0; i < n; i++)
printf(“%d “, arr[i]);
Logic To Sort Array
To sort the given array there are many algorithms available to do it in an optimized way but in this article, we will see some understandable logic that anyone can understand easily.
So let’s take an array that contains some random numbers,
for example:
int arr[] = { 80, 60, 12, 72, 8, 78, 44, 39 };
then to sort that array we will use a nested for loop in which the upper loop will hold the base index and this base index is further compared with all the next values in that array inside that nested loop and base index will be incremented again and again till the end of the array.
eg:
for (i = 0; i < n; i++) {
currentValue = pta + i;
for (j = i + 1; j < n; j++) {
nextValue = pta + j;
if (*nextValue < *currentValue) {
temp = *currentValue;
*currentValue = *nextValue;
*nextValue = temp; }
}
}
Final Code
#include <stdio.h>
// Function to sort the numbers using pointers
void sortingList(int n, int* pta)// pta stands for pointing to address and n is the total no of values in the array or array length.
{
int i, j;// tracing variables for for loop
int temp;// temp stands for temporary variables
int* currentValue;// used to hold the current value at the address.
int* nextValue;// used to hold the nextValue in that array
// sorting part starts from here
for (i = 0; i < n; i++) {
currentValue = pta + i;// pta + i will give a address + 0 = address in first case and *address will result in to value at that address.
// intializing i to value zero and incrementing it till n
for (j = i + 1; j < n; j++) {
//intializing j to i + 1 value so that there will be comparison between unique values like
nextValue = pta + j;
if (*nextValue < *currentValue) {
temp = *currentValue;
*currentValue = *nextValue;
*nextValue = temp;
}
}
}
// array data after sorting list using sortList() function
printf("Updated array values: ");
for (i = 0; i < n; i++)
printf("%d ", *(pta + i));
}
// Driver code
int main()
{
int n = 8;
int a[] = { 80, 60, 12, 72, 8, 78, 44, 39 };
printf("Intiated array values: ");
for(int i = 0; i < n; i++)
printf("%d ", a[i]);
printf("\n");
sortingList(n, a);
return 0;
}