ADVERTISEMENT
ADVERTISEMENT

What is the difference between Array and Structure in C ?

Both arrays and structures are used to store a collection of data in C programming, but they have some key differences:

  1. Array is a collection of variables of the same data type (Homogenous elements), while structure is a collection of variables of different data types (Heterogenous Elements).

  2. Array uses a single identifier to represent the entire collection of elements, while structure uses multiple identifiers to represent different elements.

  3. Array elements are accessed using indexing, while structure elements are accessed using the dot operator or the arrow operator.

  4. Array elements have the same memory layout, while structure elements may have different memory layouts.

  5. Array elements are stored in contiguous memory locations, while structure elements may be stored in non-contiguous memory locations.

  6. Array is used when we need to store a collection of similar data, for example, a collection of integers, a collection of floating-point numbers, etc. whereas structure is used when we need to store a collection of dissimilar data, for example, a collection of name, age, salary of an employee, etc.

  7. Arrays are suitable for mathematical operations, while structures are suitable for storing complex data and related information.

In summary, arrays are used for storing a collection of similar data and are useful for mathematical operations, while structures are used for storing a collection of dissimilar data and are useful for storing complex data and related information. 


ADVERTISEMENT

ADVERTISEMENT