Difference Between Array and Structure


Arrays and Structures are two different types of container datatype. The most basic difference between an array and a structure is that an Array can contain the elements of same datatype, while a Structure is a collection that can contain the elements of dissimilar datatypes.

Read this article to learn more about Arrays and Structures and how they are different from each other.

What is an Array?

An array refers to a collection that consists of homogenous elements, i.e. of same data type. Arrays are declared using '[]'. It uses subscripts/ '[ ]' (square brackets) to access the elements. An array is basically a pointer that points to the first element of a collection.

The size of an 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.

Arrays are considered as a primitive data type. In an array, it is quick and easy to traverse through and search for elements. Array elements are stored in contiguous memory locations.

Syntax of Array

data_type array_name[size];

What is a Structure?

A structure is a collection that consists of elements of heterogeneous or dissimilar data types. Structures can be declared using the 'struct' keyword, and they use the '.' (dot operator) to access the elements.

Unlike an array, the size of a structure is not fixed. This is because the elements in a structure can be of different data types and sizes. Structures are user-defined datatypes, however, it can be slow and complex to traverse and search through a structure. Structure elements may or may not be stored in contiguous memory locations.

Syntax of Structure

struct sruct_name {
   data_type1 ele1;
   data_type2 ele2;
};

Now, let us discuss the differences between array and structure in detail.

Difference between Array and Structure

The following are the important differences between array and structure −

S.No.

Array

Structure

1.

An array is a collection that consists of elements of homogenous, i.e. of same data type

A Structure is a collection that consists of elements of heterogeneous or dissimilar data types.

2.

Array is declared using '[]'.

Structure is declared using the 'struct' keyword.

3.

Array uses subscripts or '[ ]' (square brackets) to access the elements

Structure uses the '.' (dot operator) to access the elements.

4.

The size of an array is fixed

The size of a structure is not fixed

5.

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

Traversing through and searching for elements in a structure is slow and complex.

6.

An array is always stored in contiguous memory locations.

A structure may or may not be stored in contiguous memory locations.

Conclusion

The most significant difference that you should note here is that an array contains all the elements of homogeneous data types, while a structure contains the elements of heterogeneous data types.

Updated on: 20-Feb-2023

13K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements