Tuesday, August 23, 2011

Difference between Arrays and Structures in C

Both the arrays and structures are classified as structured data types as they provide a mechanism that enable us to access and manipulate data in a relatively easy manner. But they differ in a number of ways listed in table below:

Arrays
Structures
1. An array is a collection of related data elements of same type. 1. Structure can have elements of different  types
2. An array is a derived data type 2. A structure is a programmer-defined data type
3. Any array behaves like a built-in data types. All we have to do is to declare an array variable and use it. 3. But in the case of structure, first we have to design and declare a data structure before the variable of that type are declared and used.
4. Array allocates static memory and uses index / subscript for accessing elements of the array.4. Structures allocate dynamic memory and uses (.) operator for accessing the member of a structure.
5. Array is a pointer to the first element of it5. Structure is not a pointer
6. Element access takes relatively less time.6. Property access takes relatively large time.


17 comments: