Introduction:
In C programming, a structure is a powerful tool that allows you to group related variables under a single name. These variables can be of different types, and each is identified by a name within the structure. Think of a structure as a way to conveniently organize various pieces of information.
Defining a Structure:
To create a structure, you define a new named type. This not only organizes your variables but also extends the range of variable types. A structure can include other structures, arrays, or pointers as its members.
struct ComplexNumbers {
double real;
double imag;
} complex;
Here, we've created a structure ComplexNumbers to represent complex numbers, which consist of real and imaginary parts.
Practical Uses of Structures:
Structures are not just about grouping data; they are also excellent for representing specific types of information. For instance, in mathematics, complex numbers inhabit a two-dimensional plane. This spatial relationship can be easily represented using a structure.
struct Point {
double x;
double y;
} point;
Here, we've defined a structure Point to hold the coordinates of a point in a two-dimensional space.
Syntax:
struct structure_name {
// Data member list
};
Example 1: Storing Employee Information
struct Employee {
int id_number;
int age;
float salary;
};
int main() {
struct Employee employee;
employee.age = 22;
employee.id_number = 1;
employee.salary = 12000.12;
}
In this example, we've created a structure Employee to store information about an employee, making it easier to manage related data.
Example 2: Defining the Simplest Structure
#include <stdio.h>
int main() {
struct {
int a;
int b;
} x, y;
x.a = 10;
y = x; // Assigning one structure to another
printf("%d", y.a);
return 0;
}
Here, we've defined an anonymous structure x with two integers and demonstrated how to assign one structure to another.
Example 3: Working with Struct
#include <stdio.h>
struct Student {
char *name;
float number;
} student1, student2;
int main() {
struct Student student3;
char s1[30];
float f;
scanf("%s", s1);
scanf("%f", &f);
student1.name = s1;
student2.number = f;
printf("Name: %s\n", student1.name);
printf("Number: %f\n", student2.number);
return 0;
}
In this example, we've created a structure Student to represent student information, showcasing the practical usage of structures.
Example 4: Define and Use a Struct
#include <stdio.h>
struct Person {
int id;
char *name;
};
int main() {
struct Person people[2];
people[0].id = 1;
people[0].name = "Person 1";
people[1].id = 2;
people[1].name = "Person 2";
printf("ID: %d, Name: %s\n", people[0].id, people[0].name);
printf("ID: %d, Name: %s\n", people[1].id, people[1].name);
return 0;
}
Here, we've used an array of structures to store information about people, showcasing how structures can efficiently manage data in a collection.
Conclusion:
Incorporating structures in C empowers programmers with a robust mechanism for organizing and managing diverse data types. From simple examples to practical applications, understanding structures elevates code clarity and efficiency, making it an indispensable skill in the C programmer's toolkit.