The array and pointers are derived data types that have lots of differences and similarities. In some cases, we can even use pointers in place of an array, and arrays automatically get converted to pointers when passed to a function. So, it is necessary to know about the differences between arrays and pointers to properly utilize them in our program.
What is an Array?
An array is a data structure that represents a collection of elements of the same type stored in contiguous memory locations. It provides a way to store and access multiple values of the same data type using a single variable name.
- It stores a homogeneous collection of data.
- Its size can be decided by the programmer.
- It provides random access.
The array is one of the most used data types in many programming languages due to its simplicity and capability.
What is a Pointer?
A pointer is a variable that holds the memory address of another variable. Pointers can be used for storing addresses of dynamically allocated arrays and for arrays that are passed as arguments to functions.
- This size of the pointer is fixed and only depends upon the architecture of the system.
Pointers are one of the core concepts that provide a lot of unique and useful features in our program.
Difference between Arrays and Pointers
|
1. | It is declared as -: *var_name; | It is declared as -: data_type var_name[size]; |
2. | It is a variable that stores the address of another variable. | It is the collection of elements of the same data type. |
3. | We can create a pointer to an array. | We can create an array of pointers. |
4. | A pointer variable can store the address of only one variable at a time. | An array can store a number of elements the same size as the size of the array variable. |
5. | Pointer allocation is done during runtime. | Array allocation is done during compile runtime. |
6. | The nature of pointers is dynamic. The size of a pointer in C can be resized according to user requirements which means the memory can be allocated or freed at any point in time. | The nature of arrays is static. During runtime, the size of an array in C cannot be resized according to user requirements. |