Jargons
In the realms of technical jargons here is the definition of pointers in C programming language;
In computer science, a pointer is an object in many programming languages that stores a memory address
I actually copied and pasted that(lol) from here
The problem with the above definition is, even seasoned programmers find it hard to understand and they get lost, sometimes. Here is the thing, I will make a bet, if by the end of this article you don't understand pointers then i owe you, what I will tell at the end. When i say "you" here I mean mostly programmers, begginers or advanced.
A HOUSE AND ADDRESS
Every house has an address, every house has a description - say color, the architectural pattern(the way its built) and sometimes the type. The thing is even an empty land in a street has an address reserved for it(99.9 percent of the time).
Now Picture this scenario, John Doe asks for the descrtiotion of your house, you can either give him the address(A much better and definite way) or a description. One problem of giving him a description is , there is a near to certainty chance that another house might look same as your house or John Doe could easily get confused while looking for the house. But with the address he cannot miss it, he scribbles the house address on paper and takes a look when searching. Keep this near to kindergaten story in mind. Yes, you are right, a house , an address, and empty land space
THE ANALOGY
The street is C, the house is called the value of a space(take the space as say a variable - like a container), the address is stored in the pointer. There can be no same pointer for a value, yes just as there can be no same address for two houses(worst case one will be No 7a, while the other No7b, never same). If you scribble the samne address in two differntb papers, its kind of wasteful , don't you think?? In programming we try to prevent waste, hence one pointer for one address.
However, there can be same type of houses in a street, yes there can same type of values having different pointers and different addreses, in other words, each variable gets a unique memory address unless explicitly made to reference the same address(a small but manageable twist).
Also just as using pointers in C is more optimised so also if you remember, giving your exact address John Doe is much better than describing the house(especially if there are many houses in your street). This is one of many reasosn, I dare argue the core reason why C is a very quick and Efficient programming lanaguage. Linus Benedict Torvalds, the designer of the Linux os, once said
I have never seen a language that is as efficient as C to work with operating systems or machines
APOLOGIES, SOME JARGON
In c here is how it is written as shown in the image below;
- *ptr here is a pointer
- &var here is the address
- So *ptr is like the paper containing the exact address(&var is the address in memory)
- The **ptr is a pointer pointing to a pointer, take it as say you need to keep the paper containing the address somewhere, you need to know exactly where that paper is, so it now has its own location or address.
- The int here is just a neccessary part for completion it just means we are dealing with numbers(integers)
While this is an over simplified analogy, even seasoned programmers will k now this is a start. I sincerly hope this clarifies some misunderstanding.
Funfact: C programming language is one of the backbones of NASAβs flight software
Top comments (0)