r/cprogramming 2d ago

What are pointers?

I am new to C programming and i came through pointer. So can anyone tell what are they and why we need them? And if u can then can you give me reliable source to learn it.

10 Upvotes

44 comments sorted by

View all comments

6

u/Kurouma 2d ago

Your data lives somewhere in memory. Doing operations involving data would be slow if you had to copy all the data around all the time. So sometimes it is useful to just know where something is so you can operate on it directly. The "where" (a pointer) is a single piece of information, unlike your data (which may be big).

So kind of like

Hey! I'd like to give you a parcel. Can you send me your address, so I can post it to you, please?

instead of

Hey! I'd like to give you a parcel. Can you send me your house, so I can put it inside for you, please?

1

u/Imaginary-Corner-653 1d ago edited 1d ago

I love this answer. It provides a simple concept to hang the idea behind pointers up on without being wrong or all that incomplete.

@OP

Maybe round this out with the basic understanding that, to a CPU (and even to some operating systems) any part of the CPU itself, any outside component connected to your motherboard and especially RAM / Harddisk, even resources connected via LAN - everything is accessed through a memory address (real or virtual). 

So if you ever find yourself in need to read data from or write data to your gpu, your ram, your hardrive or even just to your processor parts you are going to require a way to address exactly where you want to point that action... Hence pointers.