12/21/19

Notes for computer science and engineering branch. Subject Computer programming through c language


UNIT- 7

STRUCTURES AND UNIONS

Basic of Structures
 Structures variables
 Initialization
 Structure assignment
 Structures and arrays: arrays of structures,

Basic of Structures: -

    1.   Structure is a user defined data-type.
    2.   Structure is a complex data-type.
    3.   Structure is a collection of heterogeneous variables.
    4.   Structure allows to store both primitive and derived data-types i.e. arrays and pointers at one place under one name.
    5.   Structure allows to carry different types of variables at a time.

SYNTAX: -
          
struct [<structure- tag- name>]  {here, struct is a key-word}
        {
               data-type variables;
               data-type variables;
        }
        [structure variables];
To access the structure members we should have to use the following syntax
structure variable . structure members

It is called accessing/ invoking the structure members.
Here . (dot) operator is called: -
    1.   Member access operator.
    2.   Field access operator.
    3.   Member of operator.
    4.   Membership operator.
     5.   Belongs to operator.
EXAMPLE: -
         struct stu      <------------------------ tag name
         {
                 int id;                                 {int, char, float are structure members}
                 char name[100];
                 float fee;
         }
         s1, s2;       <------------------------ structure variables


Structures and arrays: arrays of structures: -

    1.   Perhaps the most common usage of structures is in arrays of structures.
    2.   To declare an array of structures, we must first define a structure and then declare an array variable of that type.
    3.   For example, to declare a 100-student array of type student

struct  student
{
         int  rno;
         char name[50];
         int m1, m2, m3, total;
float average;
                 };
                 struct student s[100];
                
UNION: -
    
    1.   Union is a keyword.
    2.   Like the structure union are also used to place several variables of different data-type under one name. But the main difference is all union members are having common memory/ one memory.
    3.   Size of structure is sum of all the structures members but union size is biggest union members.
    4.   Union is a user defined.

SYNTAX: -
         union [<union- tag- name>]  
        {
               data-type variables;
               data-type variables;
        }
        [union variables];

Example: - FINDING UNION SIZE
#include<stdio.h>
#include<conio.h>
void main()
{
    union stu;
{
             int id;
             char name[50];
float fee;
} s;
void main
{
         clrscr();
         printf(“Union size is %d bytes”, sizeof(s));
         getch();
}

Output: -
Union size is 50 bytes

No comments:

Post a Comment

Please do not enter any spam link in the comment box and use English and Hindi language for comment.

Latest Update

Queue data structure

Popular Posts