Structure: An overview

  • Structure is one of the important concept in C programming language as it offers good functionality and ease of dealing with large data.
  • In this post we are going to discuss concept of structure and how to deal with when structure is involved in an easy to use manner.
  • A structure is a user-defined data type.
  • It can be used to store related data items as a single unit.
  • A structure is a collection of either similar or different data type items.
  • It is similar to record that stores related information about an entity.
  • Structures are similar to arrays which store similar data items and referred by a common name but an array can store only similar data type items while structure can store both similar as well as different data items together.
  • Structure elements are referred to as structure member.
  • It can be used to represent complex data items in an easy and more meaningful way.
  • Since structure is a user-defined data type, it needs to be declared before using it.
  • We have to tell the compiler what kind of our requirement is and how it can be fulfilled.
  • We can compare a structure with a structure/plan of house drawn by the engineer on the paper first to build the home.
  • Plan describes the requirement stated by the owner such as number of floors, total rooms, height, width and length of rooms etc.
  • Once the plan is ready engineer uses it to build the real house or the building as per the plan.
  • The same plan can be used to build as many houses as we want each of the same physical shape and structure.
  • The same concept is applicable in the case of programming.
  • By declaring a structure, programmer provides a blue print about his/her requirement to the compiler and then after it can be used to create a variable as per the structure definition just like we create a variable of basic data types such as int, float, char.
  • Compiler allocates memory only when a structure variable is created.
  • It acts as a template that can be used for creating variables later.

Difference: Structure v/s Array

Array Structure
1. An array is a derived data type. A structure is a user-defined data type.
2. An array is a collection of similar data type items. A structure is a collection of different data type items.
3. Memory for the array will be allocated at the time of its declaration. Memory is allocated only at the time of structure variable declaration.
4. Array elements are referred by array index. Structure elements are referred by its variable name using period sign with structure variable.
4. It is difficult to represent complex related data using array. It is easy to represent complex and related data using structure.
5. Array does not have any special keyword to declare it instead it used [] bracket to define array size along with data type. “struct” keyword is used in structure declaration.
6. Syntax:
data type arr_name [size];
Sytanx :
struct struct_name
Type member1 ;
Type member2 ;