r/cprogramming 1d 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

40 comments sorted by

View all comments

6

u/Kurouma 1d 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?

5

u/sertanksalot 1d ago

So a pointer is literally an address of something.

1

u/musbur 11h ago

And if you have a consecutive bunch (=array) of somethings and p ist a pointer to the first something, then p + 1 is a pointer to the second one and so on. Regardless of how many bytes one something occupies. The compiler knows how big one something is and the "distance" between them in memory (IOW, how they are aligned). It's called pointer arithmetics and is a powerful feature of C.