Chapter 8 Structure in C
Chapter 8 Structure in C
Important Question
Explain Structure with example Explain Union with example. State diff between Array and Structure State diff between Structure and Union
Topic Covered
Introduction Declaration Accessing Initialization Copying & Comparing typedef and its use in Structure Declaration Nesting of Structure
Topic Covered
Arrays of Structure Initializing Arrays of Structure Arrays within Structure Structures and Pointers Structures & Functions
Introduction
Arrays require that all elements be of the same data type. Many times it is necessary to group information of different data types. An example is a materials list for a product. The list typically includes a name for each item, a part number, dimensions, weight, and cost. C and C++ support data structures that can store combinations of character, integer floating point and enumerated type data. They are called a structs.
Structure
Structure is user defined data type which is used to store heterogeneous data under unique name. A structure is a collection of variables under a single name. These variables can be of different types, and each has a name which is used to select it from the structure. A structure is a convenient way of grouping together pieces of related information. Keyword 'struct' is used to declare structure. A single struct would store the data for one object. An array of structs would store the data for several objects. The variables which are declared inside the structure are called as 'members of structure'.
General Form
Struct <structure_tag_name { <data_type member_name1>; <data_type member_name2>; . . . . }<structure_variable>,<structure_var iable2>..
Explanation
The structure_tag_name is the name of the structure The structure_variables are the list of variable names separated by commas Each of these structure_variable name is a structure of type structure_tag_name Each member_name declared within the braces is called a member or structure element.
Declaration
There are three different ways to declare and/or define structure
Variable structure Tagged structure Type-defined structure
Variable Structure
struct { member_list }variable_identifie; struct { int x; int y; }a;
Tagged Structure
The tag name is referred to in subsequent declarations of variables of the type so defined. Each such declaration MUST include the keyword struct AND the name of the user-defined structure type AND the variable name(s).
struct tag_name { member_list }variable_identifie; struct s1 { int x; int y; }a;
Where Declare???
The proper place for structure declarations is in the global area of the program before main() This puts them within the scope of the entire program and is mandatory if the structure is to shared by function.
Accessing
In C program for accessing the elements of structure use dot operator('.'). For accessing element you need to the structure variable. The general format is : <structure_variable>.<member_name>; It has the same precedence as () and [], which is higher than that of any unary and binary operator. Like () and [], it associates from Left to Right.
Demo
Initialization of Structure
A structure can be initialized in much the same way as any other data type. This consists of assigning some constants to the members of the structures.
Default Value
For integer and float : 0 (zero) For char and string type : \0
DEMO
ARRAY VS STRUCTURE
Union
Union is user defined data type used to stored data under unique variable name at single memory location. Union is similar to that of stucture. Syntax of union is similar to stucture. But the major difference between structure and union is 'storage.' In structures, each member has its own storage location, whereas all the members of union use the same location. Union contains many members of different types, it can handle only one member at a time. To declare union data type, 'union' keyword is used.
Declaration
Syntax: union union_name { <data-type> element 1; <data-type> element 2; <data-type> element 3; }union_variable;
Example: union techno { int comp_id; char nm; float sal; }tch;
Accessing
DEMO
Output
Structure Vs Union