Intro

Only available on StudyMode
  • Topic: C, Data types, Java
  • Pages : 24 (7464 words )
  • Download(s) : 27
  • Published : December 8, 2012
Open Document
Text Preview
Structures, Unions, Enumerations and Bit-fields

1

Copyright © 2010 Dorling Kindersley (India) Pvt. Ltd.

Programming in C—A Practical Approach Structures, Unions, Enumerations and Bit-fields Introduction • Arrays are used for the storage of homogeneous data. • Hence we have user defined data types like structures, unions, and enumerations to store data with different types. • One of the similarities between arrays and structures is that both of them contain a finite number of elements. Thus, array types and structure types are collectively known as aggregate types. • Unions are similar to structures in all aspects except the manner in which their constituent elements are stored. • In structures, separate memory is allocated to each element, while in unions all the elements share the same memory. • Enumerations help you in defining a data type whose objects can take a limited set of values. 2 Copyright © 2010 Dorling Kindersley (India) Pvt. Ltd.

Programming in C—A Practical Approach Structures, Unions, Enumerations and Bit-fields Structures A structure is a collection of variables under a single name and provides a convenient way of grouping several pieces of related information together. It can be used for the storage of heterogeneous data. Three important tasks of working with structures: • Defining a structure type i.e. creating a new type. • Declaring variables and constants (i.e. objects) of the newly created type. • Using and performing operations on the objects of the structure type.

3

Copyright © 2010 Dorling Kindersley (India) Pvt. Ltd.

Programming in C—A Practical Approach Structures, Unions, Enumerations and Bit-fields Defining a Structure The general form of structure type definition is: [storage_class_specifier][type_qualifier] struct [structure_tag_name] { type member_name1[, member_name11, …]; [type member_name2[, member_name22, …]]; ……… } [variable_name];

4

Copyright © 2010 Dorling Kindersley (India) Pvt. Ltd.

Programming in C—A Practical Approach Structures, Unions, Enumerations and Bit-fields Important Points about Structure Definition:

1. The terms enclosed within the square brackets are optional and might not be present in a structure definition statement. But the term in BOLD is mandatory. 2. A structure definition consists of the keyword struct followed by an optional identifier name, known as structure tagname, and a structure declaration-list enclosed within the braces.

5

Copyright © 2010 Dorling Kindersley (India) Pvt. Ltd.

Programming in C—A Practical Approach Structures, Unions, Enumerations and Bit-fields struct book //Structure tag-name is book { char title [25]; //Structure declaration-list char author[20]; int pages; float price; }; struct //Structure tag-name not present { char title[25]; //Structure declaration-list char author[20]; int pages; float price; }; 6 Copyright © 2010 Dorling Kindersley (India) Pvt. Ltd.

Programming in C—A Practical Approach Structures, Unions, Enumerations and Bit-fields 3. The structure definition defines a new type, known as structure type. After the definition of the structure type, the keyword struct is used to declare its variables. 4. Since the tag-name of a structure is an identifier, all the rules for writing an identifier name are applicable for writing the structure tag-name. 5. The newly created type (i.e. tag name of the defined structure) is visible, after its definition, only in the scope in which it is defined. Hence, it is not possible to declare objects of the defined structure type outside the scope in which it (i.e. its tag name) is visible. 6. The newly created type is incomplete until the closing brace of structure declaration-list is encountered. 7 Copyright © 2010 Dorling Kindersley (India) Pvt. Ltd.

Programming in C—A Practical Approach Structures, Unions, Enumerations and Bit-fields 7. The structure declaration-list consists of declarations of one or more variables, possibly of...
tracking img