Introduction to Structure
A structure is a collection of variable under a single name.
struct structure _ name { data _ type member_ variable1; data_ type member _ variable2; . . . . . . . . data _ types member_ varables; };
Comparison of array and structure
Array structure
1. An array is a collection of related data elements 1. A structure can have elements of different types.
of same type.
2. An array is derived data type. 2. A structure is a user- defined data types.
3. Array name is pointer to the first elements of it. 3. Structure name is not pointer.
Function and Structure
Passing structure members to a function
void display(char empName[], int id, float sal) { print f("Name\t\tID\t\tsalary"); print f("%s\t\t%d\t\t%.1f", empName, id, sal); } int main() { struct employee { char name[20]; int id; float salary; }; struct employee emp; print f("Employee Name:"); scan f("%s", emp.name); print f("Employee id:\t"); scan f("%d", &emp.id); print f("salary of the Employee"); scan f("%f", &emp. salary); print f("The Entered Information of the Employee is"); display("emp.name, emp.id, emp. salary); getch(); return 0; } Output Employee Name: Ram Employee id: 100 Salary of the Employee: 15000 The entered Information of the Employee is Name Id salary Ram 100 15000.0
Passing an array of structure to a function
Write a program to create a structure named student that has name, roll, marks, and remarks as its members. Assume appropriate types and size of members. Use the structure to read and display records of four students. Create two functions: one is to read information of students and other is to display the information on screen. Pass an array of structure to above functions as their arguments.
#define size 4 int i; struct student { char name[30]; int roll; float marks; char remark ; }; void display(struct student st []) { print f("Student Name\t Roll\t marks\t Remarks"); for(i=0;i<size;i++) print f("%s\t\t %d\t %.2f\t %c\n", st [i]. name, st [i]. roll, st [i].marks, st[i].remarks); } void read (struct student stu[]) { float tempForMarks ; for(i=0;i<size;i++) { print f("Enter Information of student No %d", i+1); print f(" Name:\t"); scan f("%s", stu[i]. name); print f(" Roll:\t"); scan f("%d", stu[i]. roll); print f(" Marks"); scan f("%f", &tempfForMarks); stu[i].marks= tempFormarks; print f("Remarks(P/F):\t"); stu[i]. remark = getche(); } } int main() { struct student s[size]; float tempForMarks; print f("Read information of Employee from user"); read(s); print f("The Detail Information is"); display(s); getch(); return 0; } Output Read information of Employee from user Enter Information of student No 1 Name: Hari Roll: 101 Marks: 56 Remarks(P/F): p Enter Information of student No 2 Name: Bina Roll: 102 Marks: 67 Remarks(P/F): p Enter Information of student No 3 Name: Gita Roll: 103 Marks: 89 Remarks(P/F): p Enter Information of student No 4 Name: Krishna Roll: 104 Marks: 12 Remarks(P/F): f The Detail Information is Student Name Roll Marks Remarks Hari 101 56 p Bina 102 67 p Gita 103 89 p Krishna 104 12 f
Union
The union use the shared memory space and so handle only member at a time, while different memory will be reserved for each structure member and so can handle operation on more members at a time.
Example
Create a union named student that has roll and marks as member. Assign some values to these members one at a time and display the result one at a time.
int main() { union student { int roll; float marks; }; union student st; st. roll= 455;\ print f("Roll=%d", st.roll); st.marks=78; print f("Marks=%f", st.marks); getch(); return 0; } Output Roll= 455 Marks= 78
Difference between Structure and union
Structure Union
1. It takes more memory space than that of union. 1. It takes less memory than that of structure.
2. All the members of structure can be accessed at 2. Only one member of union can be accessed at any
any point of time. given time.