Introduction to Structure in C

Class12 NEB -Computer Science Exam for Computer Science

Posted by yanib on 2025-03-16 08:51:58 |

Share: Facebook | Twitter | Whatsapp | Linkedin Visits: 223


Introduction to Structure in C

A structure in C is a user-defined data type that allows you to group different types of data together under a single name. This is useful when you want to store multiple attributes (such as name, age, salary) of a person or object, but each attribute may be of different types (like string, integer, float, etc.).

Syntax to Define a Structure:

c
struct structure_name { data_type member_variable1; data_type member_variable2; /* more members */ };

Example of a Structure:

c
struct employee { char name[20]; int id; float salary; };

In the above example:

  • struct employee is the structure definition.
  • name, id, and salary are the members of the structure employee, where name is a string (char array), id is an integer, and salary is a floating-point number.

Comparison of Array and Structure

ArrayStructure
1. An array is a collection of related data elements of the same type.1. A structure can have elements of different types.
2. An array is a derived data type.2. A structure is a user-defined data type.
3. Array name is a pointer to the first element.3. Structure name is not a pointer.

Function and Structure

Passing Structure Members to a Function

You can pass structure members to a function in C in the same way as individual variables.

Example: Passing structure members to a function:
c
#include <stdio.h> #include <conio.h> void display(char empName[], int id, float sal) { printf("Name\t\tID\t\tSalary\n"); printf("%s\t\t%d\t\t%.1f\n", empName, id, sal); } int main() { struct employee { char name[20]; int id; float salary; }; struct employee emp; printf("Employee Name: "); scanf("%s", emp.name); printf("Employee ID: "); scanf("%d", &emp.id); printf("Salary of the Employee: "); scanf("%f", &emp.salary); printf("The Entered Information of the Employee is:\n"); display(emp.name, emp.id, emp.salary); getch(); return 0; }

Output:

yaml
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 Structures to a Function

You can pass an array of structures to a function as an argument. Here's an example that works with an array of structures.

Example: Handling an Array of Structures:
c
#include <stdio.h> #include <conio.h> #define SIZE 4 struct student { char name[30]; int roll; float marks; char remark; }; void display(struct student st[]) { printf("Student Name\t Roll\t Marks\t Remarks\n"); for(int i = 0; i < SIZE; i++) { printf("%s\t\t %d\t %.2f\t %c\n", st[i].name, st[i].roll, st[i].marks, st[i].remark); } } void read(struct student stu[]) { for(int i = 0; i < SIZE; i++) { printf("Enter Information of student No %d:\n", i+1); printf("Name: "); scanf("%s", stu[i].name); printf("Roll: "); scanf("%d", &stu[i].roll); printf("Marks: "); scanf("%f", &stu[i].marks); printf("Remarks (P/F): "); stu[i].remark = getche(); printf("\n"); } } int main() { struct student s[SIZE]; printf("Read information of students from user:\n"); read(s); printf("\nThe Detail Information is:\n"); display(s); getch(); return 0; }

Output:

yaml
Read information of students 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 in C

A union is similar to a structure, but it uses the same memory location for all its members. So, only one member can hold a value at any time.

Union Example:

c
#include <stdio.h> int main() { union student { int roll; float marks; }; union student st; st.roll = 455; printf("Roll: %d\n", st.roll); st.marks = 78.0; printf("Marks: %.2f\n", st.marks); return 0; }

Output:

makefile
Roll: 455 Marks: 78.00

In the above example:

  • The union student has two members: roll and marks.
  • When we assign a value to one member, it overwrites the value of the other, because both members share the same memory location.

Difference Between Structure and Union

StructureUnion
1. Takes more memory (separate memory is allocated for each member).1. Takes less memory (memory is shared by all members).
2. All members can be accessed at any time.2. Only one member can be accessed at any time.
3. Memory size is the sum of the sizes of all members.3. Memory size is the size of the largest member.

Conclusion

  • Structure allows you to store multiple different types of data together, while a union saves memory by sharing the same memory space for all its members.
  • Structures are ideal when you need to store different types of data that should exist simultaneously, while unions are more memory-efficient when you only need to store one member at a time.

Let me know if you need any further clarification!

Leave a Comment: