C is one of the most powerful programming languages and a must learn for anyone interested in programming. Unlock the full power of C and C++ by learning pointers in C and C++.
Pointers are special variables that are used to store addresses. Let’s say, you have 5 values stored in the memory of your computer. A pointer points to the address of the memory space where a certain value is stored. A variable stores the value of an object. But a pointer points to the address where the value is stored in the memory. For example, in order to point to A, a pointer will point to its memory address 1000.
Now to point to D the pointer will point to its address 1003.
Let’s say, we have a variable X. And the value of the variable X is 7.
The pointer pointing to X is not 7. The pointer pointing to X is the base address where the value 7 is stored.
It’s like if you are holding a pen for me in your hand, instead of saying I want the pen, I am saying I want whatever you have in your hand. So I am pointing to the hand that is holding the pen. Pointers work something like this.
Let’s see how pointers work in programming. We have talked about the variable X that has value 7. We will now define that. This is how we define X.
int X = 7;
Let’s say, the value of X, i.e. 7 is stored in the memory location 1000. In order to learn about pointers we need to learn about two operators, & and *.
Operator & will give us the address of the variable. & operator is ‘address of’ operator. So,
&X = 1000.
Operator * will give us the content of an address. * operator is ‘content at’ operator. So,
*(&X) = *(1000) = 7.