C-Programming

Structures

We are very much familiar with array, which is used to store the data of same type in a single variable different in subscript.  But let’s think if we have to store different types of data items in a single unit i.e, which shares a common name as we can store the same type of data in an array, then what we will do? No need to worry about this, because for this, C has another option called structure which is a method of packing of data of different types. Structures are very useful and important in handling the logically related data items. Array makes the program user friendly, in the same way structure makes the program more user friendly.

A structure is a collection of one or more variables, possibly of different types, grouped together under a single name for convenient handling. (Structures are called ``records'' in some languages, notably Pascal.) Structures help to organize complicated data, particularly in large programs, because they permit a group of related variables to be treated as a unit instead of as separate entities.

One traditional example of a structure is the payroll record: an employee is described by a set of attributes such as name, address, social security number, salary, etc. Some of these in turn could be structures: a name has several components, as does an address and even a salary. Another example, more typical for C, comes from graphics: a point is a pair of coordinate; a rectangle is a pair of points, and so on.

The main change made by the ANSI standard is to define structure assignment - structures may be copied and assigned to, passed to functions, and returned by functions. This has been supported by most compilers for many years, but the properties are now precisely defined. Automatic structures and arrays may now also be initialized.

A structure is a heterogeneous mixture of different data items of different data types that can be represented by a common name. This means it can store same or different types of data items in a single unit as the array stores the different data items of same type in a single name.

DYNAMIC MEMORY ALLOCATION (Prev Lesson)
(Next Lesson) How to declare the structure variable?
Back to C-Programming

No Comments

Post a Reply

Teacher
Bhim Gautam
Role : Lecturer
Read More
error: Content is protected !!