This content originally appeared on DEV Community and was authored by Tomislav Kraljic
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!
Variables
Why do we declare variables in programming? This may sound like a silly question on the surface but there is more to it!
The memory address of data is a hexadecimal value.
It would be tedious, difficult to read and annoying if we had to use the hexadecimal value every time.
This is where variables come in. Instead of "0x9FFF0", we could give a meaningful name. If we named "0x9FFF0" to age, now it is easier to work with. Under the hood, it is still "0x9FFF0" and the computer understands it because it is still a valid address but for human readability and productivity, we just set it to a specific and meaningful name to work with.
Code Example
This content originally appeared on DEV Community and was authored by Tomislav Kraljic
Tomislav Kraljic | Sciencx (2021-04-24T06:08:55+00:00) What are Pointers in C/C++?. Retrieved from https://www.scien.cx/2021/04/24/what-are-pointers-in-c-c/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.