Pointer vs array in C
Generally, in most cases, users assume that the pointer and array are both the same thing. However, it is not that simple because we will find some distinct differences if we look deeply at both. Therefore in this article, we will see whether the pointer and array are really the same or not.
Definition of array
Arrays are generally used for storing the same types of data items or values that are same in the nature. Although arrays cannot be used to store the data items or values that are not same in the nature, it is also considered a significant disadvantage of the arrays.
Definition of the pointers
Apart from the arrays, if we talk about the pointer, they are generally used for storing the memory address of the other variable instead of storing the actual value of the variable. However, we can also use the pointer to access the whole array by using the pointer arithmetic. In addition, it also even makes the accessing array process much faster.
The pointer variable can be as declared as shown below:
What are the main differences between pointers and arrays?
Apart from what we have discussed above, the key differences can be found while implementing the pointer and array. For example, when the arrays are implemented, the fixed size of the memory block is allocated. On the other hand, where the pointers are implemented, the memory is dynamically allocated. Thus, allocating the memory in both the pointers and arrays can be considered the key difference. However, it is not the only difference that lies between the arrays and pointer because some other differences also do exist that are as follows:
If we use the "sizeof(array)", it will return the amount of memory used by all elements stored in the array. However, if we use the it for the pointer, e.g. "sizeof(pointer)", it only returns the amount of memory used by the pointer variable itself.
Lets us see the quick comparison chart to understand the difference between both arrays and pointers in an easier manner:
In this article, we have explained the key differences between Array and Pointers. Therefore, after reading this article, we hope that your confusion about pointer and array will be cleared forever.