Pointer 1: Intro to the 👉Pointers👈
Tags: productivity, beginners, tutorial
Published: 2024-01-13 17:21:48

Here, we will talk about the basic idea of Pointer in C programming. 
## What is a pointer?

Like any variable, **Pointer** is a variable like a reference that holds a memory to another variable, arrays. 

And many tasks are easily done by pointers. Like, 
       • Always works on the original variable. 
       • We can easily create any data structure.
       • Searching and sorting huge data very easily. 
       • Returning more than one value in the functions.
 
Now let’s see the basic and simple form of pointers. Suppose there is an integer variable age, and we want to print the memory location of the variable and also the age.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6ggcesl13ev0p3gjpqsz.png)
 
%p is used to display the memory address, and the operator & is used to display the memory location. The output will be:
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yb8bw9r74mcfpxwaoftm.png) the memory location will give the hexadecimal.

We can also store this ‘0061FF1C’ address in a separate variable, like a reference, which is the pointer. Now, to make a pointer, we have to make sure the pointer variable is the same as the variable we are pointing to. If the variable is an integer, then the pointer type will be an integer; if the variable is a character, then the pointer type will be a character.


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

Here, pAge has its own address, but the value stored within it is another address, and we can store it using the **‘*’ or the indirection operator**.

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

Here, we print the address stored in pAge using %p.



## POINTER DECLARATION AND POINTER INITIALIZATION


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/043abg6x5pk0ces6yxn6.png)

If we break it down, then `int *pAge;` is declaring the pointer, and `int *pAge = &age;` is initializing the pointer, i.e we are storing the memory location of variable age in the pointer pAge.

## DEREFERENCING

Now, suppose we want to access the value at the stored address. We can simply do it using the **‘*’ or the indirection operator**. Simply put, we want to find the value at the address, which is called **dereferencing**.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2yulpikvq3bd1kti0287.png)

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

## POINTERS ONLY USING THE OPERATORS 

There is another way by which we can find the value stored in a memory address without assigning a pointer using '%d', '%c', or ‘%f’ based on the type of variable. For example.


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


**OUTPUT** 
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l2nlg3m0rumboz7e4l3o.png)

## Size of pointers 

Now, in C, the size of the pointer varies according to its type.
for example, the size of the int, char, double or float type is different



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


**OUTPUT**


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

So, from here, we can see that the size of each variable is 8 bytes, but when we assign it to a pointer variable, the size of an integer and a float is 4 bytes, the size of a character is 1 byte, and the size of a double type is 8 bytes.

 

##EXAMPLES
Check here for few basic examples of [Pointers](https://github.com/Ridwan805/Pointers/tree/main/1%20Basics%20of%20pointer) 

## MORE


Since the pointer in C stores the memory address, its size is independent. It depends on the system's architecture. 
Pointers are used in many other cases. For example.

- To pass the values by reference 

- To do system-level programming where memory addresses are useful.

- To access the values of an array easily.

- To return Multiple values from a function.

And many more other uses, which we will talk about later.