Structures & Unions

v  STRUCTURES
Structure permits a programmer to design new data type variables; using the basic data types.
A structure data type called student can hold all this information:
                                struct student
                                                {
                                                char name[25];
                                                int rollno;
                                                float totmarks;
                                                char grade;
                                                };
 The above is a declaration of a data type called student. It is not a variable declaration, but a type declaration.

Initializing Structure Members
Structure members can be initialized at declaration.
This is similar to the initialization of arrays; the initial values are simply listed inside a pair of braces, with each value separated by a comma.
s1= { “Varun”, 53, 515, ‘A’ };
                The structure declaration is preceded by the keyword static
OR
 static struct student s1= { “Varun”, 53, 515, ‘A’ };

  v  MEMORY SPACE???
              
                struct student
                                                {
                                                char name[25];
                                                int rollno;
                                                float totmarks;
                                                char grade;
                                                };
                student s1;
25B + 2B + 4B + 1B = 32B

  • Structures & Pointers 


WAP for finding Passed or Failed ?
                struct student
                {
                char sname[15];
                int rollno;
                int totmarks;
                };
main()
{
                int m;
                struct student s , *pqr;
                s = {“Anil”, 12, 475”};
                pqr = &s;
m = pqràtotmarks;
                                if(m > 210)
                                {
                                printf(“PASSED”);
                                }
                else
                                {
                                printf(“FAILED”);
                                }
                }


WAP for printing the Product name and Price ?
                struct  product
                {
                char pname[3];
                int price;
                };
main()
                {
                struct product  pro[25], *ptr;
                printf(“Enter product name and their price”);
                                for(i=0;i<25;i++)
                                                {
                                scanf(“%s%d”,pro[i].pname, &pro[i].price);
                                                }
ptr = pro;   // initializing the pointer with starting address
i=0;
print(“ Details of  Product s are”);
while(i<25)
                {
printf(“%s%d”, ptràpname, ptràprice);
                ptr++;
                }
}

  
  v  STRUCTURES & FUNCTIONS

struct student
{
char sname[15];
int rollno;
int totmarks;
};
main()
{
struct student s;
                s = {“Anil”, 12, 475”};
                displayGrade(&s);
}
displayGrade(struct student *pqr)
{
                int m;
                m = pqràtotmarks;
                                if(m > 210)
                                {
                                printf(“PASS”);
                                }
                else
                                {
                                printf(“FAILED”);
                                }
}
  
  v  Introduction to Unions

Unions are C variables whose syntax look similar to structures, but act in a completely different manner.
A union is a variable that can take on different data types in different situations
The union syntax is:
                                                union tag_name {
                                                type1 member1;
                                                type2 member2;
                                                …
                                                };
For example, the following code declares a union data type called XYZ and a union variable called U1:
union XYZ
{
float f;
int i;
};
union XYZ  U1; 

Unions and Memory

• Once a union variable has been declared, the amount of memory  reserved is just enough to be able to represent the largest member
  (Unlike a structure where memory is reserved for all members).
• In the previous example, 4 bytes are set aside for the variable U1 since a  float will take up 4 bytes and an int only 2.
• Data actually stored in a union’s memory can be the data associated with any of its members. But only one member of a union can contain valid data at a given point in the program.
• So a user must keep track of which type of data has most recently been stored in the union variable.

MEMORY SPACE??? 

 Now, to find the memory space required for the union variable, one has to look for the member variable requiring maximum space.
     
                union EMPLOYEE
                                                {
                                                char name[15];
                                                int empno;
                                                char dept[10];
                                                };
                union EMPLOYEE  e;

Is it  à15B + 2B + 10B = 27B ???

No comments:

Post a Comment