Difference Between Array and Pointer

Computer ProgrammingProgrammingMiscellaneous

In this post, we will understand the difference between arrays and pointers.

Array

  • It stores value of a variable that has a homogeneous data type.

  • It can generate an array of pointers.

  • It supports the array concept.

  • An array of pointers can be generated.

  • It refers to a collection that consists of elements of homogenous/same data type.

  • It uses subscripts/ ‘[ ]’ (square brackets) to access the elements.

  • It is a pointer that points to the first element of the collection.

  • The array objects can’t be instantiated.

  • The size of array is fixed based on the number of elements in the array.

  • This size is the product of number of elements and size of every element.

  • Bit field is not possible in an array.

  • It is considered as a primitive data type.

  • Traversing through and searching for elements in an array is quick and easy.

  • It can be declared using the ‘[ ]’.

  • Array size is fixed and is basically the number of elements multiplied by the size of an element.

  • It is stored in contiguous memory locations.

In C++, it can be declared as follows −

Syntax

type var_name[size];

In Java, it can be declared as follows −

Syntax

type var-name[ ];
var_name = new type[size];

Pointer

  • It stores address of variables.

  • It can only store address of one variable at a point in time.

  • A pointer to an array can be generated.

  • It can be initialized to any value.

  • It can be initialized any time after its declaration.

  • It can be assigned to point to a NULL value.

  • It can be dereferenced using the ‘*’ operator.

  • It can be changed to point to a different variable of the same type only.

Example

int val = 5;
//code//
int *p = &val;
raja
Published on 24-Mar-2021 14:05:34
Advertisements