Note-: I have taken help from Yashvant Kanetkar Let Us C book and tutorialspoint site for covering this concept. You can visit tutorialspoint site or can buy Let Us C for deep understanding of C Language.
You must have learned about functions and passing value between them. Consider ,
sum=add(l,m);
Here we are passing values of variables to the called functions. Also, when we define any variable like int n; the compiler will assign a memory location(address) for the variable in the memory. Now instead of passing values to the function we can pass this address of variable to the function for processing.
When we pass the variable values to the function it is called “call by Value” and when we are passing the address of variable to function it is called “call by reference”. For understanding call by reference you must understand the concept of pointers.
Pointers-:
Suppose we declare a variable as,
int j=2;
Here compiler will assign a storage location(address) in memory named as j for the variable. In this address, value of i i.e., 2 will get stored. Let’s assume that address at which value of j stored is 65727. Pictorial view of storing value is shown below-:

For printing the location number(address) coding is-:
#include<stdio.h> // Assuming that address of location is 65727.
int main()
{
printf(“Address of j is %u\n”, &j);
printf(“Value of j is %d\n”, j);
}
OUTPUT-:
Address of j is 65727
Value of j is 2
Here statement printf(“Address of variable is %u\n”, &j); is returning address of j i.e., at which location value of j is stored in memory. %u is format specifier for unsigned integers value. This was a basic program to make clear you about address of variables and printing it. Now let’s understand concept of pointers.
In C we use “*” operator which is called pointer and it denotes the “Value at address”. Using * will provide you value stored at particular address. Let’s understand it with an example-:
#include<stdio.h>
int main()
{
int j=2;
printf(“Address of j is %u\n”, &j);
printf(“Value of j is %d\n”, *(&j));
}
OUTPUT-:
Address of j is 65727
Value of j is 2
Here value *(&j) given value at address &j i.e., 2. If we store this address in another variable.
k=&j;
Now k stores address of variable j. Also k must be given a storage location which means k must be having it’s own address. You will understand this easily from below pic-:

Here address of j is stored as value of k at address of k. Before using j we must declare it.
int *k; is official statement to declare a pointer variable in C program. This will tell compiler that k will be storing address of an integer value. So we can say that-:
“Pointers are the variables which stores address of a particular variable in it.“
Now take a look at these statements-:
int *soul;
float *mortal;
char *pubg;
Here soul, mortal, pubg are pointer variables i.e., they store address of variable. char *pubg; means pubg is containing the address of char value and float *mortal means mortal is containing address of floating point value. It does not mean that mortal is going to contain a float value. Let’s consider an example for understanding pointers perfectly.
#include<stdio.h>
int main()
{
int j=2;
int *k;
k=&j;
printf(“Address of j is %u\n”, &j);
printf(“Address of j is %u\n”, k);
printf(“Address of k is %u\n”, &k);
printf(“Value of k is %u\n”, k);
printf(“Value of j is %d\n”, j);
printf(“Value of j is %d\n”, *(&j));
printf(“Value of j is %d\n”, *k);
}
OUTPUT-:

Note-: “Addresses that are coming in ouput screen are actual addresses of variable. We have assumed 65727 and 65522 as addresses just for explaining. Do not get confuse in different address of variable.“.
So that’s all in this post. Comment down below if you have any doubt to ask and do follow us on facebook, twitter, LinkedIn and Instagram for getting latest updates and more C Programming notes.
Leave a comment