C pointers all you need to know
Published: 2022-09-07 18:29:24

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

1. Building operating systems - Microsoft Windows kernel
2. Embedded systems
3. 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**.
<br>
## **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.

<br>
--
### **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
1. name.
2. address
3. value
4. type

<br>
--
### **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}; `👎<br>
To store different data types we use **structures**.
<br>
### **STANDARD LIBRARY**
`<stdio.h>`
Standard Input Output library has the input and output functions.

input function : `scanf()` ` getc()` etc<br>
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>
```
<br>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.


<br>
--
# 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.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/o8a3ftg215furdg5l7ko.jpeg)

<br>_In the number variable_
data stored: 22
address of number variable: 2000<br>

_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<br>
<br>
> 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.


<br>Now we are ready to tackle any type of pointers💪.Let us dive into the code…

<br>

***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`<br><br>

Here 18 is stored(initialised) in the number variable and the address of the number variable is stored in the numberPointer using & operator.<br>

> `numberPointer = &number;`


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0moja0h0rv03jq0xhezu.jpeg)

**%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**.<br>
<br>


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ug84el6dte2l8h3sw5ms.jpg)
<br>

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7p4phsvudjpdsab8yr51.jpg)
<br>
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`



![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yk0q5n5jra6ls33yk6cu.jpeg)
<br>
--
### 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<br>
 `&` -Address of operator<br>
 `*` -Indirection operator<br>

<br>
--
### 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;
 }
```


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/k2vpt5kzmxlhrkzbmaoz.jpg)

**output:** 
`6422036 
6422036
 18
 18`
<br>
> `*(*doublePointer) = *(6422036)`
> while looking into 6422036 address 18 value is accessed.

<br>
--
## Conclusion
This article is done with the basics of pointers<br>
1. pointers definition and usage
2. pointers and addresses
3. single pointer
4. double pointer
<br>
 <br>but you shouldn't be done with pointers. Play with them. Best Wishes.
<br> Before I leave, I would like to leave you with the following comic!


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6z7fadx823s0cmahybw6.jpeg)

**NEVER STOP EXPLORING!!**