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:
-
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).
-
Array uses a single identifier to represent the entire collection of elements, while structure uses multiple identifiers to represent different elements.
-
Array elements are accessed using indexing, while structure elements are accessed using the dot operator or the arrow operator.
-
Array elements have the same memory layout, while structure elements may have different memory layouts.
-
Array elements are stored in contiguous memory locations, while structure elements may be stored in non-contiguous memory locations.
-
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.
-
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.