Structure

  • Why do we need a structure?
  • We have seen till now that a variable can be of single data type either an int or float or char etc.
  • But if we want to store information consisting of different data types as a single entity then what do we do?
  • Consider the example where we want to store the information of an employee of an organization and the employee details included employee number, employee name his address etc.
  • So C provides a data type called ‘structure’. A structure gathers together, different types of information that comprise a given entity.
  • A structure is a collection of one or more variable, possibly of different types grouped together under a single name for the convenience.
  • Syntax to declare a structure is:
     	 		struct name of the structure {
    						  structure elements;
    						  };
    Example:
    			 	struct employee  {
    						      int emp_no;
    						      char emp_name[25];
    						      };
    
  • Once the structure data type has been defined one or more variables can be declared to be of that type.
  • For example the variables e1, e2, e3, e4 can be declared to be of the type struct employee as struct employee e1,e2, e3,e4;
  • The above statement set aside 27 bytes of memory for each of the variable e1, e2, e3 and e4 as each of the variable need to store an integer value and a string i.e. two bytes for integer and a character array of 25 bytes.


    Different ways of defining the structures and their variables

  • We can also combine definition of structure type and the declaration of the variables in one statement.
     
    Example:
    			   struct employee  {
    						     int emp_no;
    						     char emp_name[25];
    					                } e1, e2, e3, e4;
    
  • If we are combining definition of structure type and the declaration of the variables in one statement then we can even omit the name of the structure type. Example: struct{ int emp_no; char emp_name[25]; } e1, e2, e3, e4;
  • Structure variables can also be initialized at the time of declaration.
    Example:
    				struct employee  {
    						     int emp_no;
    						     char emp_name[25];
    					                } ;
    struct employee e1= { 101, “Krithika”};
    struct employee e1= { 101, ‘Gokul”};
    Example: 
    				struct employee  {
    						     int emp_no;
    						     char emp_name[25];
    					                } 
    e1, e2,e3= { 101, “Krithika”};
    
  • Note: In the above example only the variable e3 is initialized.
  • We can even assign the value of one structure variable to another. Example: struct employee { int emp_no; char emp_name[25]; } e1, e2,e3= { 101, “Krithika”}; e2=e3; Note:
  • Semicolon must follow the closing brace of the structure type definition
  • A structure type defines only the form of a structure and compiler will not reserve any space in memory


    Accessing the structure elements

  • To access the elements of structure dot operator is used along with the structure variable. Example: e1.emp_no; e1.emp_name;
  • Whatever be the elements of a structure, they are always stored in contiguous memory locations.
  • The example given reads the elements of the structure and displays the elements.
    	# include < stdio.h >
    	main ( )
    	{
    	struct employee
    	{
    	int emp_no;
         char emp_name [25];
    	};
    	struct employee e1;
    	printf (“Enter the Employee number and name\n”);
    	scanf (“%d%s”,&e1.emp_no,&e1.emp_name);
    	printf(“Employee Number is %d and Name is %s\n”,e1.emp_no, e1.emp_name);  
    		}
    

    One structure can be nested within another structure

  • A structure can be the element of another structure, which is given in the example.
    	Example:
    struct empdt
    		{
    		char emphname [10];
    		int hnum;
    		};
    	struct employee
    		{
    		char empname [25];
    		int empno;
    		struct empdt empad;
    		} e1;
    
    Example


Strings << Previous

Next >> Preprocessor directives

Our aim is to provide information to the knowledge


comments powered by Disqus






Footer1