Mask-group

Introduction Of Structures



  • Structures is a user-defined data type that allows you to hold different types of elements.

  • Each element of a structure is called a member.

  • Structures can be widely used to store various types of information such as student information,employee information,product information,etc.

  • A structure contains a number of data types grouped together. These data types may or may not be of the same type.

Declaring a Structure:

  • The struct keyword can be used to define structure.

struct { 

structure element 1 ;

structure element 2 ;

structure element 3 ;

...... 

...... 

};

Example:

struct book { 

char name ; 

float price ;

 int pages ; 

} ; 

  • This statement defines a new data type called struct book. Each variable of this data type will consist of a character variable called name, a float variable called price and an integer variable called pages.

Examples:

Points to Remember:

  • The closing brace in the structure type declaration must be followed by a semicolon. 

  • It is important to understand that a structure type declaration does not tell the compiler to reserve any space in memory. All a structure declaration does is, it defines the ‘form’ of the structure.

  • Usually structure type declaration appears at the top of the source code file, before any variables or functions are defined. In very large programs they are usually put in a separate header file, and the file is included (using the preprocessor directive #include) in whichever program we want to use this structure type.