Wednesday, June 9, 2010

Structure

Array vs. Structure
1. An array is a collection of related data elements of same type. Structure can have elements of different data types.
2. An array is derived data type whereas a structure is a programmer-defined one.
3. Any array behaves like a built-in data type. All we have to do is to declare an array variable and use it. But in the case of a structure, first we have to design and declare a data structure before the variables of that type are declared anbd used.

Defining a structure
struct tag_name
{
data_type member1;
data_type member2;
};
1. The template is terminated with a semicolon
2. Wgile the entire difinition is considered as a statement, each member is declared independently for its name and type in a separate statement inside the template.
3. The tag name such as book_bank can be used to declare structure variables of the type, later in the program.

Declaring Structure Variables
struct book_bank, book1, book2,book3;
Elements included in declaring structure variable
1. The keyword struct
2. The structure tag name
3. List of variable names separated by commas
4. A terminating semicolon

Accessing Structure members
Members of a structure can be accesed by structure variable( member operator .) variable name

Structur Initialization
Rules for Initialization of Structure
1. We cannot initialize individual members inside the structure template
2. The order of values enclosed in braces must match the order of members in the structure difinition.
3. It is permitted to have a partial initialization. We can initialize only the first few members and leave the remaining blank. The uninitialized member should be only at the end of the list
4. The uninitialized members will be assigned default value as zero for intiger and #0 for character string

Unions
Major distinction between structure and union is in terms of storage. In structure, each member has its own storage location, whereas all the members of a union use the same location. This implies that, although a union may contain many member of different types, it can handle only one member at a time

No comments:

Post a Comment