The C language is a very powerful language and its power is because of the pointer. With the right use of a pointer, we can make compact and very effective code using a pointer. The pointer is used for returning more than 1 value from any function, for DMA (Dynamic memory allocation), to access members of an array.
A pointer holds the address of a variable. We can say that a pointer points to that variable whose address it is holding.
To understand the concept of the pointer, we need to understand memory organization in computers. This is as explained below.
Memory organization in computer
It is necessary to declare the variable before using it. Because the compiler has to reserve the space for that variable in memory. And declaring its data type with variable declaration is also necessary because the data type of the variable shows how much space is to be reserved in memory for that variable.
Let us understand it by example. We are declaring “a” variable if type int. Graphical representation is given below.
So for this int variable, the compiler reserves 2 consecutive bytes in memory. The address of the first byte from these two bytes is called the address of variable “a”. Suppose the compiler has reserved bytes numbered 288 and 299 for the storage of variable “a”. So the address of variable “a” will be 288. If we give any value to “a” then this value will be stored in these two bytes. This was for the int variable. For char, float variables it holds consecutively 1 and 4 bytes in memory.
Learning from this blog:
- Meaning of pointer.
- What value does the pointer hold?
- How memory is allocated to any variable in memory.
- How can we find the address of any variable in memory?
- Memory is allocated by different data types.