What are Pointers in C?
In this article, I will explain what pointers are and how to use them. But to understand pointers, we need a quick overview on how a computer handles/stores data.
RAM
RAM (Random Access Memory) is a component in your computer that stores data.
Here is what it looks like:
- RAM allows for direct-access to memory cells.
- These memory cells store data in a binary-format
- Each memory cell has an address where the data can be accessed.
- A pointer just "points" to that specific memory address where that data is stored.
Street Address Analogy
Now, that you understand what RAM is, how data is stored and accessed, this analogy will make more sense. Think of the RAM in your machine as a long street full of houses. Each house is different, unique and has its own address. Now, let's say you are expecting mail. Well, how does the mailman know where to bring your mail? Well, every house has a mailbox with a specific address on it with your house number. This specific house address is the "pointer". It says "Hey!, I am right here!."So, What is a pointer?
In short, pointers are special data types just that store the memory address rather than a value.
To create a pointer variable, all we do is a put an * before the variable name. Simple as that!