C Structure
In C programming, a structure is a user-defined data type that allows grouping together variables of different types under a single name. This allows us to organize data in a way that reflects the relationship between different values, making the code easier to maintain and understand.
Definition
A structure is defined using the struct
keyword, followed by the structure name, and then the variables (also called members) it will contain.
Syntax:
struct structure_name {
data_type member1;
data_type member2;
...
};
Here, structure_name
is the name of the structure, and member1
, member2
, etc., are the members that can be of different data types.
Example of Structure Declaration and Initialization
The following example demonstrates how to define a structure to represent a student with multiple members, such as name, age, and grade. It also shows how to initialize and access structure members.
Example 1:
#include <stdio.h>
struct Student {
char name[50];
int age;
char grade;
};
int main()
{
// Declare and initialize a structure variable
struct Student student1 = {"John Doe", 20, 'A'};
// Accessing structure members
printf("Name: %s\n", student1.name);
printf("Age: %d\n", student1.age);
printf("Grade: %c\n", student1.grade);
return 0;
}
Output:
Age: 20
Grade: A
Explanation of Example 1
In this example:
- The structure
Student
is defined with three members:name
,age
, andgrade
. - The structure is initialized using the values
"John Doe"
,20
, and'A'
for thename
,age
, andgrade
members respectively. - We then access the structure members using the dot (
.
) operator and print the values.
Structure Initialization
You can initialize structure variables in two main ways:
- Static Initialization: Where the values of structure members are explicitly provided at the time of declaration (like in Example 1).
- Dynamic Initialization: Where values are assigned to the structure members during runtime.
Example of Dynamic Structure Initialization
The following example demonstrates how to assign values to a structure after declaration:
Example 2:
#include <stdio.h>
struct Student {
char name[50];
int age;
char grade;
};
int main() {
struct Student student2;
// Dynamic Initialization
printf("Enter name: ");
fgets(student2.name, sizeof(student2.name), stdin); // Using fgets for safe input
printf("Enter age: ");
scanf("%d", &student2.age);
printf("Enter grade: ");
scanf(" %c", &student2.grade); // space before %c to consume any leftover newline character
// Accessing structure members
printf("nStudent Details:n");
printf("Name: %s\n", student2.name);
printf("Age: %d\n", student2.age);
printf("Grade: %c\n", student2.grade);
return 0;
}
Output:
Enter age: 22
Enter grade: B
Student Details:
Name: Alice Green
Age: 22
Grade: B
Explanation of Example 2
In this example:
- The structure variable
student2
is declared but not initialized at the time of declaration. - The values for
name
,age
, andgrade
are input dynamically from the user usingfgets()
andscanf()
. - After the values are assigned, we print the details of the student to the screen.
Nested Structures
Structures can also contain other structures, which is known as a nested structure. Here's an example:
Example 3: Nested Structures
#include <stdio.h>
struct Date {
int day;
int month;
int year;
};
struct Student {
char name[50];
int age;
struct Date birthdate; // Nested structure
};
int main() {
struct Student student1 = {"John Doe", 20, {5, 7, 2002}}; // Initialize nested structure
printf("Name: %s\n", student1.name);
printf("Age: %d\n", student1.age);
printf("Birthdate: %02d/%02d/%d\n", student1.birthdate.day, student1.birthdate.month, student1.birthdate.year);
return 0;
}
Output:
Age: 20
Birthdate: 05/07/2002
Explanation of Example 3
In this example:
- The structure
Student
contains another structureDate
as a member. This is an example of a nested structure. - The nested structure
birthdate
is initialized with values forday
,month
, andyear
. - We then access and print the nested structure's values along with the other members of the
Student
structure.
Key Points
- A structure allows grouping variables of different types together.
- Structure members can be of any data type, including other structures.
- Structures can be initialized statically or dynamically.
- Accessing structure members requires using the dot operator (
.
).