Chapter 10: Structures
Structures in C allow you to create custom data
types that can hold multiple variables of different types. They are used to
group related data together and create more organized and complex data
structures. In this chapter, we will explore how to define, access, and use
structures in C:
10.1
Structure Declaration:
To declare a structure in C, you need to define its
structure tag and list the member variables inside curly braces.
struct
structure_name {
data_type member1;
data_type member2;
// ...
};
Example:
#include
<stdio.h>
struct
student {
char name[50];
int age;
float
gpa;
};
int
main() {
// Structure variable declaration
struct student s1;
// Structure variable initialization
strcpy(s1.name, "John");
s1.age = 20;
s1.gpa = 3.8;
// Accessing structure members
printf("Name: %s\n", s1.name);
printf("Age: %d\n", s1.age);
printf("GPA: %.2f\n", s1.gpa);
return 0;
}
The program declares a structure `student` that
contains three member variables: `name`, `age`, and `gpa`. It declares a
structure variable `s1` and initializes its members. It then accesses and
prints the values of the structure members. Compile and run the program to see
the output.
10.2
Nested Structures:
Structures can be nested inside other structures to
create more complex data structures.
Example:
#include
<stdio.h>
struct
date {
int day;
int month;
int year;
};
struct
student {
char name[50];
struct date birthdate;
float gpa;
};
int
main() {
struct student s1;
strcpy(s1.name, "John");
s1.birthdate.day = 15;
s1.birthdate.month = 7;
s1.birthdate.year = 1999;
s1.gpa = 3.8;
printf("Name: %s\n", s1.name);
printf("Birthdate: %d/%d/%d\n",
s1.birthdate.day, s1.birthdate.month, s1.birthdate.year);
printf("GPA: %.2f\n", s1.gpa);
return 0;
}
The program defines a nested structure `date` to
represent a date. The `student` structure contains a member variable
`birthdate` of type `date`. The program initializes and accesses the members of
the nested structure. Compile and run the program to see the output.
10.3
Typedef:
You can use the `typedef` keyword to create a new
name for a structure type. This simplifies the declaration of structure
variables.
Example:
#include
<stdio.h>
typedef
struct {
char name[50];
int age;
float gpa;
}
Student;
int
main() {
Student s1;
strcpy(s1.name, "John");
s1.age = 20;
s1.gpa = 3.8;
printf("Name: %s\n", s1.name);
printf("Age: %d\n", s1.age);
printf("GPA: %.2f\n", s1.gpa);
return 0;
}
The program uses `typedef` to create a new name
`Student` for the structure. It declares a structure variable `s1` using the
`Student` type. It then initializes and accesses the members of the structure.
Compile and run the program to see the output.
10.4
Passing Structures to Functions:
You can pass structures to functions as arguments to
manipulate or access their members.
Example:
#include
<stdio.h>
struct
point {
int x;
int y;
};
void
printPoint(struct point p) {
printf("Point coordinates: (%d,
%d)\n", p.x, p.y);
}
int
main() {
struct point p1 = {3, 5};
printPoint(p1);
return 0;
}
The program defines a structure `point` representing
a point in a Cartesian coordinate system. It declares a function `printPoint()`
that takes a `point` structure as an argument and prints its coordinates. The
program calls the function with a `point` structure as an argument. Compile and
run the program to see the output.
0 Comments