Structure


  • We have seen that variables holds one piece of information and how array holds information of similar datatype.These two variables handle great variety of situation but sometimes we often come across condition where we have to deal with entities that are collection of dissimilar datatypes.
  • suppose we want to store information of students of a class we may need to store name ( a string ) , roll no. ( an integer ) , age ( an integer ) , results ( a float ) . To deal with such problems we use structure.

What Is A Structure ??

  • A structure is a collection of element which may not be of similar datatype.
  • it differs from array as an array is collection of elements of similar datatype.
  • The individual elements of structure are referred as member structure.

Syntax :

  • A structure specification may be given as
    struct tag
    {
    member 1 declaration;
    member 2 declaration;
    ...
    ...
    member m declaration;
    }
  • In this declaration struct is required keyword, tag is a name that specifies structure of this type ( i.e. structure having this composition ) .
  • The members can be ordinary variables, pointers, arrays or other structure. The member names within a particular structure must be distinct from one another , though a member name can be the same as the name of a variable defined outside of the structure.
  • A storage classes cannot be assign to an individual member and individual members cannot be intialized within a structure.

Syntax :

  • Once the structure is specified , individual structure type variables can be declared as follows:
    storage class struct tag variable 1 , variable 2 ,.... , variable n;
    where storage class is optional and specifies the storage class of the variable , struct is required , tag is the name that apppeares in the structure . specification and variable 1 , variable 2 , .... , variable n are structure variable of the type tag.
  • It is possible to combine the variable declaration with structure specification as shown below . Storage-class struct tag
    {
    member 1 declaration;
    member 2 declaration;
    ...
    ...
    member m declaration;
    }variable1 , variable 2 , .... , variable n;

    The tag is optional when we combine structure variable declaration with specification

    • Print Page