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 enables us to access and manipulate data in a relatively easy manner. But they differ in a number of ways listed in the table below:

Arrays
Structures
1. An array is a collection of related data elements of the 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. An 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.

Let me explain each point one by one with an example of each.

First, an array is a collection of related data elements of the same type. You can't have an array of different data types in C (you can do this in Python). The following program throws an error in C

int array[5] = {1, 2, "string", 4, 5};




However, in Structure, you can have elements of different data types. The following program compiles without an error.

struct Person {
char name[10];
int age;
float height;
};

Second, an array is a derived data type. Derived data type means they are derived from fundamental data types. An array can be a collection of int, char, float, double, ... but it is not a data type in itself. It adds the additional functionality to the existing data types. But, a structure is a user-defined data type. A user has all the right to create a data type to satisfy his needs. For example, the user may need a data type to represent a book.  Can a book be represented by fundamental data type? Can a book be represented by an int? or Float? or String? the integer can be used represent the total number of pages,  similarly, the float can be used to represent the price of the book and string can be used to represent the title and author of the book. But one integer alone can't represent a whole book but it requires a combination of different fundamental data types.

struct Book {
char title[50];
char author[50];
float price;
int pages;
};

A structure is, therefore, a new data type. Just like int, char or float, structure creates a new data type.
This means we can have an array of structures just like we can have an array of integers. The following code snippet creates an array of structure Book.

struct Book books[2];

'books' is an array of data type 'Book' which can contain two books.

Here are other related articles related to Array

  1. Difference between Array and Pointer in C/C++
  2. Maximum and Minimum elements of an Array



19 comments: