Difference Between Array and Pointer


Array and pointers have a very close relationship in programming, but there are several differences between them. Read this article to find out how Arrays are different from Pointers. But let us first discuss some basics of arrays and pointers.

What is an Array?

An array is one that stores the values of a homogeneous data type. It refers to a collection that consists of elements of homogenous/same data type. Arrays are considered as a primitive data type. They are declared using the '[ ]' and are stored in contiguous memory locations. Also, arrays use subscripts/ '[ ]' (square brackets) to access the elements. Array objects cannot be instantiated.

The size of an array is fixed based on the number of elements in the array. Array size is basically the number of elements multiplied by the size of an element. Another important point is that the Bit field is not possible in an array. In an array, traversing through and searching for elements is quick and easy.

Syntax

The following is the syntax to declare an array −

type var_name[size];

What is a Pointer?

A pointer is one that is used to store the address of variables. A pointer can store the address of one variable at a time. We can generate a pointer to an array.

Unlike arrays, pointers can be initialized to any value while defining them. Also, they can be initialized at any time after their declaration. Pointers can also be assigned to point to a NULL value.

A pointer is dereferenced using the '*' operator. It can be changed to point to a different variable of the same type only.

Syntax

The following is the syntax to declare a pointer in C language −

datatype *variable_name;

Difference between Array and Pointer

The following table highlights the important differences between arrays and pointers −

S.No.

Array

Pointer

1.

It stores the values of a homogeneous data type.

It stores the address of variables.

2.

An array is defined as a collection of similar datatypes.

Pointer is a variable which stores address of another variable.

3.

The number of variables that can be stored is decided by the size of the array.

Pointer can store the address of only a single variable.

4.

The initialization of arrays can be done while defining them.

Pointers cannot be initialized while defining them.

5.

The nature of arrays is static.

The nature of pointers is dynamic.

6.

Arrays cannot be resized according to user's requirements.

Pointers can be resized at any point of time.

7.

The allocation of array is done at compile time.

The allocation of pointer is done at run time.

Conclusion

The most significant difference that you should note here is that arrays are used to store the values of a homogenous datatype, whereas pointers are used to store the addresses of variables.

Updated on: 20-Feb-2023

8K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements