Showing posts with label Structures. Show all posts
Showing posts with label Structures. Show all posts

Monday, 27 October 2014

How to Declare a Structure Variable?

Declaring a Structure Variable:

--> By defining a structure we have only created a format, the actual use of structures will be when we declare variables based on this format.

--> We declare structure variables in 2-ways:
 
       1)With Structure definition
       2)Using the Structure tag

1.With Structure definition:-

                            struct student
                            {
                                      char name[20];
                                      int rollno;
                                      float marks;
                             }stu1, stu2, stu3;

--> Here stu1, stu2, stu3 are variables of type struct "student". when we declare a variable while defining the structure template, the tagname is optional.

--> So, we can also declare them as:

                            struct
                            {
                                       char name[20];
                                       int rollno;
                                       float marks;
                             }stu1, stu2, stu3;

--> If we declare variables in this way, then we will  not be able to declare other variables of this structure type anywhere else in the program

--> So, although the tagname is optional  it is always better to specify a "tagname" for the structure.

2. Using Structure Tag:

            We can also declare structure variables using structure tag. This can be written as:

                            struct student
                            {
                                      char name[20];
                                      int rollno;
                                      float marks;
                             };
                             struct student stu1,  stu2;
                             struct student stu3;


--> Here stu1, stu2 and stu3 are structure variables that are declared using the structure tag "student".

--> Declaring a structure variable reserves "space in memory". Each structure variable declared to be of type struct student has 3 members. name, rollno, marks.

--> The "compiler" will "reserve space" for each variable sufficient to hold all the members.

Example:
 
                 each variable of type struct student will occupy 26(20+2+4) bytes.




Next




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.