Friday 11 January 2019

UNIONS


UNIONS
Unions are similar to structures except the fact that memory allocated for a union variable is memory required for the largest field.
The only difference between the structure and union is memory allocation.
Syntax:
          Union unionname
          {
                   Datamember1;
                   Datamember2;
          };

Example

#include<stdio.h>
#include<conio.h>
union emp
{
          char name[25];
          int idno;
          float salary;
}e1;
void main()
{
          printf("Size of union is %d",sizeof(e1));
}

Output
Size of union is 25

Example
#include<stdio.h>
#include<conio.h>
union emps
{
          double a;
          float b[2];
          char c[8];
}e1;
void main()
{
          int i;
          clrscr();
          e1.a=1355.674;

          e1.b[0]=2.3;
          e1.b[1]=4.5;

          printf("Enter 8 characters string:");
          for(i=0;i<8;i++)
                   scanf("%c",&e1.c[i]);
          printf("%.3lf\n",e1.a);
          printf("%.2f,%.2f\n",e1.b[0],e1.b[1]);
          for(i=0;i<8;i++)
                   printf("%c\n",e1.c[i]);

          getch();
}
Output:
Enter 8 characters string: asdgghjk
2.713061780997133810000000000000000000000e+209
1078827923249377390000000.00,283381667918681262000000000.00a
s
d
g
g
h
j
k

it allocates memory only to the largest data type size. In this situation it allocates memory to string, the rest two values double and float values are garbage values.

STRUCTURES


STRUCTURES
An array is a collection of elements of similar type. Many a times we need to group multiple types of data into a single entity. To do this, we use structures.
Ex:     students details:    sno, sname, age, course, fee
          Employee details:   eno, ename, age, basic

If it is an array we have to take number of arrays like sno to hold all sno’s, and sname to hold all sname, age to hold all of the ages which is time consuming it increases the code, etc. in such situations we declare a structure which is capable of holding all these items in a single entity.
STRUCTURE is a collection of variables under a single name. A structure is a collection of hetrogenious elements.
Declaration of a structure
General format:     struct tag_name
                             {
                                      datatype member1;
                                      datatype member2;
                                      ----
----
                             };

Ex:                        struct student
                             {
                                      int sno,age,fee;
                                      char sname[20],course[20];
                             };
          This time no memory will be allocated to structure. This is only the definition of structure that tells that there exists a user-defined data type student. Using this structure we have to create the structure variables:
                             Struct student s1,s2,s3;
Or
struct student
                             {
                                      int sno,age,fee;
                                      char sname[20],course[20];
                             }s1,s2,s3;
At this point, we have created 3 instances or structure variables of user-defined datatype student. Now memory will be allocated. The amount of memory allocated will be the sum of all data members which form part of the structure.
S1 occupies(2+2+2+20+20)=46 bytes
S2 occupies(2+2+2+20+20)=46 bytes
S3 occupies(2+2+2+20+20)=46 bytes
Totally it occupies 138 bytes
Initialization of structure
The structure initialization is followed by an (=) sign & a list of initialization values separated by commas & enclosed in braces
struct student
                             {
                                      int sno,age,fee;
                                      char sname[20],course[20];
}s1={1,23,15000,”XYZ”,”BSc”};
They are placed in the structure members in the order in which the members are listed in structure definition.
Accessing members of a structure
Structure members are accessed using the structure member operator(.) also called dot operator, between the structure name and member name.

Syntax:        structure variable.member name;
                  
Ex:              struct student
                   {
                             int sno,age,fee;
                             char sname[20],course[20];
                   }s1;

                   s1.sno=23; //accessing structure member
                   s1.sname=”gvp”;

//storing & retrieving values from structure
#include<stdio.h>
#include<conio.h>
struct student
{
          int  rno,marks;
          char name[20];
};
void main()
{
          struct student s1;
          clrscr();
          printf("Enter the roll no:");
          scanf("%d",&s1.rno);
          printf("\nEnter the name:");
          scanf("%s",&s1.name);
          printf("\nEnter the marks:");
          scanf("%d",&s1.marks);
          printf("\nRoll No is %d",s1.rno);
          printf("\nName is %s",s1.name);
          printf("\nMarks are %d",s1.marks);
          getch();
}
Output
Enter the roll no:234
Enter the name:gvp
Enter the marks:65
Roll No is 234
Name is gvp
Marks are 65
Structures with arrays
It is possible to declare an array of structures. The array will have individual structures as its elements.
Syntax:
struct person                                               struct person
{                                                                  {
          char name[25];                OR                        char name[25];
          float salary;                                                 float salary;
}per[10];                                                       };
                                                                   struct person per[10];
Here per is an array of 10 person structures.
Accessing elements in array of structures
In the above example, if you want to access the 5th structure, we can use the statement:
          per[4];

per[4].name; //access the name of 5th structure
per[0].name[5];      //access 6th character of 1 structure name member
per[2].salary //access the salary of 3rd structure
Initializing array of structures
We can initialize an array of structures in the same way as a single structure.
Ex:
struct person
{                                                                           
char name[25];               
          float salary;                                                
};
struct person per[3]=
{
          “mahesh”,3000,
          “arjun”,5000,
          “rana”,3445
};                                    
Thus, per[0] will be assigned to the first set, per[1] to the second and so on.
Example
#include<stdio.h>
#include<conio.h>
struct empl
{
       int eno;
       char name[20];
};
void main()
{
          struct empl e1[10];
          int n,i;
          clrscr();
          printf("Enter the no of employees:");
          scanf("%d",&n);
          for(i=0;i<n;i++)
          {
                   printf("Enter the %d empno and name:",i+1);
                   scanf("%d%s",&e1[i].eno,e1[i].name);
          }
          for(i=0;i<n;i++)
                             printf("Empno and name is:%d\t%s\n",e1[i].eno,e1[i].name);
          getch();
}
Output:
Enter the no of employees:3
Enter the 1 empno and name:1 gvp
Enter the 2 empno and name:2 gvp1
Enter the 3 empno and name:3 gvp2
Empno and name is:1     gvp
Empno and name is:2     gvp1
Empno and name is:3     gvp2

Structures with functions

C supports the passing of structure values as arguments to functions in three methods:
  1. Pass each member of the structure as an actual argument. But this becomes unmanageable and inefficient when the structure size is large.
  2. Pass the copy of an entire structure to the called function. Any changes to the structure members in the called function will not affect the actual structure. It is therefore necessary for the function to return the entire structure back to the calling function.
  3. Pass the address of a structure to the called function. This is similar to the way arrays are passed to functions.

General form of sending a copy of a structure to the called function is:
Syntax:       function_name(structure variable_name)
Called function takes the following form:
                   Data_type function_name(st_name)
                   struct_type st_name;
                   {
                             -----
                             -----
                             return(expression);
                   }
Structures with pointers
The address of a given structure variable can be obtained by using & operator.
Ex: &per
Per is a structure variable. Thus &per gives the address of this structure.
Arrow operator -> is used to access a field through a pointer to a structure.
Example
#include<stdio.h>
#include<conio.h>
struct student
{
          int  rno;
          char name[20];
          float fee;
};
void main()
{
          struct student s1,*p; //p is a pointer to the structure student
          clrscr();
          p=&s1;
          printf("Enter the roll no:");
          scanf("%d",&s1.rno);
          printf("\nEnter the name:");
          scanf("%s",&s1.name);
          printf("\nEnter the fees:");
          scanf("%f",&s1.fee);
          printf("\nRoll No is %d",p->rno);
          printf("\nName is %s",p->name);
          printf("\nFees are %.2f",p->fee);
          getch();
}
Organization in memory

123
Gvp
25000.00
Values
100           (&s1)
Rno
Name
Fee
Members
P              (=&s1=100)
100
102
122
Addresses
222         address of p
S1
S1
S1
Structure variable
Output
Enter the roll no:123
Enter the name:gvp
Enter the fees:25000.00
Roll No is 123
Name is gvp
Marks are 25000.00

Structures within Structures

Structure within a structure is called as nested structures.
Example
#include<stdio.h>
#include<conio.h>
struct address
{
          int  dno;
          char city[20];
};
struct student
{
          int rno;
          char name[20];
          float fee;
          struct address a;  //structure within structure
};
void main()
{
          struct student s1;
          clrscr();
          printf("Enter the roll no:");
          scanf("%d",&s1.rno);
          printf("\nEnter the name:");
          scanf("%s",s1.name);
          printf("\nEnter the fees:");
          scanf("%f",&s1.fee);
          printf("\nEnter the Door No:");
          scanf("%d",&s1.a.dno);
          printf("\nEnter the city:");
          scanf("%s",s1.a.city);
          printf("\nRoll No is %d",s1.rno);
          printf("\nName is %s",s1.name);
          printf("\nFees is %.2f",s1.fee);
          printf("\nDno is %d",s1.a.dno);
          printf("\nCity is %s",s1.a.city);
          getch();
}
Output:
Enter the roll no:123
Enter the name:sreya
Enter the fees:25000.00
Enter the Door No:4245
Enter the city:vsp
Roll No is 123
Name is sreya
Fees is 25000.00
Dno is 4245
City is vsp

typedef
typedef is a keyword which is used to define an alias name for a datatype.
Ex: typedef int length;
Length becomes an alias name to int and variables can be declared by using the new type name that has been created.
Length l1,l2;
Now l1,l2 is a variables of type length which is nothing but an alias name for int.

Example
#include<stdio.h>
#include<conio.h>
void main()
{
          typedef int gvp;
          gvp a=45,b=45;
          printf("Sum=%d",a+b);
          getch();
}
Output
Sum=90