Difference Between Array and Structure

Computer ProgrammingProgrammingMiscellaneous

In this post, we will understand the difference between array and structure.

Array

• 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.

Example

data_type array_name[size];

Structure

• It is a collection that consists of elements of heterogenous/dissimilar data types.

• It uses the ‘.’ (dot operator) to access the elements.

• It isn’t a pointer.

• It can be instantiated.

• The size is not fixed.

• This is because the elements in a structure can be of different data types and sizes.

• Bit field is possible to create in a structure.

• It can be declared using the ‘struct’ keyword.

• It is a user-defined datatype.

• Traversing and searching through a structure is slow and complex.

• They may or may not be stored in a contiguous memory location.

Example

struct sruct_name {
data_type1 ele1;
data_type2 ele2;
};
Published on 24-Mar-2021 13:31:58