C is one of the most powerful, efficient, and widely-used language in today's world even though it is one of the oldest ( since 1972 -Dennis Ritchie), it is still used in
- Building operating systems - Microsoft Windows kernel
- Embedded systems
- Compilers and video games.
In the world of C, pointers are an essential tool for developers. These pointers directly deals with our system's memory which is a great advantage and this gives us strong foundation like literally how the program and storage works together.
But now modern programming languages takes away the function of pointers from the developer's direct control(as advancement!) which makes them to lose fundamental knowledge of programs in memory management.
BEFORE YOU START
If you are a beginner then you must know about variables and data types before commencing otherwise you can skip this part.
VARIABLES
In the layman language variable means something it tends to change which is not constant always.
In a rental house the person who lives there are variables similarly here the rental house is a memory unit and the person who lives in it are the data which will vary as user changes.
Definition: Variables are containers for storing data values. A variable is a name given to that memory location which stores the data.
A variable has four attributes
- name.
- address
- value
- type
DATA TYPE
In C the variable's data type should be given to allocate the memory.
If we save the age in a variable it's data type is integer(INT). If my mark 81.5 is stored then it's data type is float…if the decimal points is way too longer then it's data type is double.
What if the user store his grade 'A', then the variable's data type is character(CHAR).If user name is stored which is a group of characters for example "Rowan Atkinson" then it's stored as character array, where array is a data structure in which we can store same type of data.
example:
int arr[] = {1, 2, 3}; // ✅
int arr[] = {1, 'A', 1.2}; // 👎
To store different data types we use structures.
STANDARD LIBRARY
stdio.h
Standard Input Output library has the input and output functions.
input function :
scanf()
getc()
etc
output function: printf()
putc()
etc
To use printf()
function in our code then we should include the below code in the beginning by convention.
#include<stdio.h>
The #include
preprocessor directive is used to paste code of given file(here stdio.h) into current file.
Now let us uncover the magic of pointers in c.
POINTERS
Pointer is a special data type where it can only store the address of the variable i. e, address means the location of the variable in the memory.
Declaring a pointer:
char *charPointer ; //This charPointer is a pointer to a char data type variable.
int *intPointer; //This intPointer is a pointer to a int data type variable.
In the number variable
data stored: 22
address of number variable: 2000
In the intPointer which is a pointer to a int data type variable
data stored: 2000 //address of the number variable
address of intPointer: 5000
Note:
int data type variable can hold integer(10) value only, not float(10.0). charPointer and intPointer are the variable names which can be changed as per user needs.
Now we are ready to tackle any type of pointers💪.Let us dive into the code…
Read the code with the below explanation
#include<stdio.h>
int main(){
int number = 18;
int* numberPointer;
numberPointer = &number;
printf("\n%d", &number);
printf("\n%d", numberPointer);
printf("\n%p", numberPointer);
return 0;
}
output:
6422036
6422036
000000000061FE14
Here 18 is stored(initialised) in the number variable and the address of the number variable is stored in the numberPointer using & operator.
numberPointer = &number;
%p is one of the format specifier where it is used to print the value in the variable as hexadecimal format(base 16). By convention, memory address is represented in hexadecimal format.
To access the value in the number variable using numberPointer the below statement is used. Here the * (deferencing operator) operator uses the value in the numberPointer(the value present in the numberPointer is the number variable's address) and goes to that address then returns us the data present in that address.
printf("\n%d", *numberPointer);
output:
18
WHY WE SHOULD USE THESE POINTERS
This can be used to change/update the values of many different variables of the same type. Execution time is also faster in the usage of pointers
The name of the two important operators used so far is
&
-Address of operator
*
-Indirection operator
THE MOST CONFUSING DOUBLE POINTERS IS CLEARED NOW:
Read the code with the below illustrative image
#include<stdio.h>
int main(){
int number = 18;
int* numberPointer;
int** doublePointer;
numberPointer = &number;
doublePointer = &numberPointer;
printf("\n%d", &number);
printf("\n%d", *doublePointer);
printf("\n%d", *(*doublePointer));
printf("\n%d", **doublePointer);
return 0;
}
output:
6422036
6422036
18
18
*(* doublePointer) = *(6422036)
while looking into 6422036 address 18 value is accessed.
Conclusion
This article is done with the basics of pointers
- pointers definition and usage
- pointers and addresses
- single pointer
- double pointer
but you shouldn't be done with pointers. Play with them. Best Wishes.
Before I leave, I would like to leave you with the following comic!
NEVER STOP EXPLORING!!