Monday, 27 October 2014

What is Structure and Explain Structure Syntax?

Structures:

Definition:

--> A "Structure" is a "Collection of related elements", possibly of "different types", having a "single name".

-->A Structure is a collection of related data elements of different data types having a single name.

-->Each element in a structure is called a "field".

-->A "field" is a smallest element of named data that has meaning. It has many of the characteristics of the variables, we have been using in our programs.

Example:
           
                Consider the book database consisting of book name, author, no. of pages and price
we define a structure to hold this information as follows:


-->The General "syntax" of structure definition as follows

                                      struct structure-name
                                      {
                                                    datatype         member 1;
                                                    datatype         member 2;
                                                    datatype         member 3;
                                                    ---------------
                                                    ---------------
                                                    datatype         member n;
                                      };

--> Here "struct" is a "keyword", which tells the compiler that a structure is being defined.

--> member 1, member 2, member 3 ------------- member n are known as the members of the structure and are declared inside curly braces.

--> There should be a "semicolon" at the end of the curly braces. These members can be of any datatype like int, char, float, array, pointer, or another structure type.

--> "structure-name" is the name of the structure and it is used further in the program to declare variables of this structure type.

--> Definition of a structure provides "one more datatype" in addition to the built in data types . We can declare variables of this new datatype that will have the format of the defining structure.

NOTE:-
                         
--> The definition of a structure does not reserve any space in memory for the member.

--> Space is reserved only when actual variables of this structure type are declared.







0 comments:

Post a Comment