Structures and Unions in C PDF
Document Details
Uploaded by ConvincingDevotion1364
GITAM Deemed to be University
Tags
Summary
This document provides an overview of structures and unions in C programming. It explains concepts like defining, creating objects, and accessing members of structures. It also touches upon using pointers with these structures. The document contains multiple examples and definitions, making it a valuable resource for learners.
Full Transcript
Structures in C A structure is a convenient way of grouping several pieces of related information together a collection of variables under a single name Structures in C The variables in structures can be of different types, and each has a name which is used to select it...
Structures in C A structure is a convenient way of grouping several pieces of related information together a collection of variables under a single name Structures in C The variables in structures can be of different types, and each has a name which is used to select it from the structure Example : ID (integer) && Name (char array) A Student record A structure is a new named type that may involve several different typed variables Defining Structures 2 struct rectangular_prism { int height; int width; int length; }; // name of new type?? struct student_record { int ID; char name; }; Structures : Creating objects struct rectangular_prism { int height; int width; int length; }; // Below the definition of structure you can create // objects from it “obj” struct rectangular_prism obj; obj height width length Structures : Creating static arrays of objects struct student_record { int ID; char name; }; // Creates an array of student records “group” struct student_record group; group group ID name group ID name group ID name group ID name Structures : Creating static arrays of objects struct student_record { int ID; char name; }; // Creates an array of student records “group” struct student_record group; group group ID name group ID name group ID name group ID name Structures : Accessing members of structures struct rectangular_prism { int height; int width; int length; }; // Create an object from the structure defined above “obj” struct rectangular_prism obj; // Members of the objects can be accessed by putting a dot // following the object name obj obj.height=10; height=10 width=15 length=40 obj.width=15; obj.length=40; Structures : Accessing members of structures struct rectangular_prism { int height; int width; int length; }; struct rectangular_prism obj; // Create a pointer to point object “obj” struct rectangular_prism *p = &obj; (*p).height=10 // or obj.height or p->height=10 obj == *p p->width=15; height=10 width=15 length=40 p->length=40; Structures : Accessing members of structures // Defines structure struct student_record { int ID; char name; }; // Creates an array of student records “group” struct student_record group; group.ID=200710; strcpy(group.name, “doddy”); group.ID=200711; group strcpy(group.name,group.name); group ID=200710 name= “doddy” group ID=200711 name =?? Definition — Structure A collection of one or more variables, typically of different types, grouped together under a single name for convenient handling Known as struct in C 22 struct Defines a new type I.e., a new kind of data type that compiler regards as a unit E.g., struct motor { float volts; //voltage of the motor float amps; //amperage of the motor int phases; //# of phases of the motor float rpm; //rotational speed of motor }; //struct motor struct Defines a new type E.g., struct motor { float volts; float amps; int phases; float rpm; }; //struct motor 24 struct Defines a new type E.g., struct motor { float volts; Members of the float amps; struct int phases; float rpm; }; //struct motor 25 Declaring struct variables struct motor p, q, r; Declares and sets aside storage for three variables – p, q, and r – each of type motor struct motor M; Declares a 25-element array of motor; allocates 25 units of storage, each one big enough to hold the data of one motor struct motor *m; Declares a pointer to an object of type motor 26 Accessing Members of a struct Let struct motor p; struct motor q; Then p.volts — is the voltage p.amps — is the amperage p.phases — is the number of phases p.rpm — is the rotational speed q[i].volts — is the voltage of the ith motor q[i].rpm — is the speed of the ith motor 27 Accessing Members of a struct (continued) This construct is so widely used that a special notation was invented, i.e., p->member, where p is a pointer to the structure 28 Previous Example Becomes … Let struct motor *p; Then p -> volts — is the voltage of the motor pointed to by p p -> phases — is the number of phases of the motor pointed to by p 29 Initialization of a struct Let struct motor { float volts; float amps; int phases; float rpm; }; //struct motor Then struct motor m = {208, 20, 3, 1800} initializes the struct 30 Initialization of a struct Let struct motor { float volts; float amps; int phases; float rpm; }; //struct motor Then struct motor m = {208, 20, 3, 1800} initializes the struct 31 Typedef Definition:– a typedef is a way of renaming a type E.g., typedef struct motor Motor; Motor m, n; Motor *p, r; …); 32 typedef (continued) typedef may be used to rename any type Convenience in naming Clarifies purpose of the type Cleaner, more readable code Portability across platforms E.g., typedef char *String; E.g., typedef int size_t; typedef long int32; typedef long long int64; 33 Unions A union is like a struct, but only one of its members is stored, not all I.e., a single variable may hold different types at different times Storage is enough to hold largest Members are overlaid on top of each other E.g., union { int ival; float fval; char *sval; } u; 34 #include #include // create struct with srudentvariable struct student { int rollno ; float cgpa; } student1; int main() { student1.rollno=1; student1.cgpa=20.33; printf("%d%.2f",student1.rollno,student1.cgpa); } #include #include struct student { int rollno ; float cgpa; }; int main() { typedef struct student stud; stud student1; student1.rollno=1; student1.cgpa=20.33; printf("%d%.2f",student1.rollno,student1.cgpa); } ` #include #include // create struct with srudentvariable struct student { int rollno ; float cgpa; }; int main() { struct student abc; abc.rollno=12; abc.cgpa=19.33; //printf("%d",student1.rollno); printf("%d%.2f",abc.rollno,abc.cgpa); } #include #include struct Person { char name; int rollno; float cgpa; } person1; int main() { strcpy(person1.name, "ritika"); person1.rollno = 1984; person1. cgpa = 7.6; // print struct variables printf("Name: %s\n", person1.name); printf("rollno No.: %d\n", person1.rollno); printf("CGPA: %.2f", person1.cgpa); return 0; } #include #include // create struct with srudentvariable struct student { int rollno ; float cgpa; }; int main() { struct student abc; abc.rollno=12; abc.cgpa=19.33; abc.rollno=13; abc.cgpa=20.33; //printf("%d",student1.rollno); printf("%d%.2f",abc.rollno,abc.cgpa); printf("%d%.2f",abc.rollno,abc.cgpa); } #include struct student { int age; float cgpa; }; int main() { struct student *sp, student1; sp = &student1; printf("Enter age: "); scanf("%d", &sp->age); printf("Enter cgpa: "); scanf("%f", &sp->cgpa); printf("Displaying:\n"); printf("Age: %d\n", sp->age); printf("cgpa: %.2f", sp->cgpa); return 0; } #include #include struct Person { char name; int rollno; float cgpa; }; int main() { struct Person person1,*sp; sp=&person1; scanf("%s",sp->name); scanf("%d",&sp->rollno); scanf("%f",&sp->cgpa); // print struct variables printf("Name: %s\n", sp->name); printf("rollno No.: %d\n", sp->rollno); printf("CGPA: %.2f", sp->cgpa); return 0; } Union #include #include struct student { int rollno ; float cgpa; }student1; int main() { printf("%d",sizeof(student1)); return 0; } #include #include union student { int rollno ; float cgpa; }student1; int main() { printf("%d",sizeof(student1)); return 0; } #include #include union student { int rollno ; float cgpa; }student1; int main() { student1.rollno=23; printf("%d",student1.rollno); student1.cgpa =25.6; printf("%.2f",student1.cgpa); //printf("%d %.2f",student1.rollno, student1.cgpa); return 0; } 1. Create a structure called "Student" with members name, age, and total marks. Write a C program to input data for two students, display their information, and find the average of total marks. Create a structure named "Employee" to store employee details such as employee ID, name, and salary. Write a program to input data for three employees, find the highest salary employee, and display their information