Unit 7 Structure
Unit 7 Structure
In C, a structure is a user-defined data type that allows you to group related data items together
under a single name. Each data item within a structure is referred to as a "member" or a "field."
Structures provide a way to organize and manage complex data in a program.
1. Defining a Structure:
To define a structure, you use the struct keyword followed by the structure name and a set of
curly braces {} containing the members of the structure.
struct Person {
char name[50];
int age;
float height;
};
In this example, we define a structure called Person with three members: name , age , and
height .
4. Initializing Structures:
You can initialize a structure when declaring it.
int main() {
struct Person person1 = {"John", 30, 5.9};
displayPerson(person1);
return 0;
}
6. Nested Structures:
A structure can contain another structure as a member, allowing for nested or hierarchical data
structures.
struct Address {
char street[50];
char city[30];
};
struct Employee {
char name[50];
int empId;
struct Address address;
};
7. Arrays of Structures:
You can create arrays of structures to handle multiple instances of the same data structure.
8. Pointer to Structure:
You can create pointers to structures and access members using the arrow ( -> ) operator.
#include <stdio.h>
#include <string.h>
int main() {
// Declare a variable of type Person
struct Person person1;
return 0;
}
In this example:
We define a structure called Person with members name , age , and height .
Output:
Person 1
Name: John Doe
Age: 30
Height: 5.90 feet
2. Example of structure
int main() {
// Declare a variable of type Point
struct Point p1;
return 0;
}
In this example:
Output:
3. Example of Structure
Certainly! Let's create a structure to represent a book with title, author, and price:
#include <stdio.h>
int main() {
// Declare a variable of type Book
struct Book book1;
return 0;
}
In this example:
We define a structure called Book with members title , author , and price .
Output:
Book Details:
Title: The C Programming Language
Author: Brian W. Kernighan and Dennis M. Ritchie
Price: $29.99
4. Example of Structure in C
#include <stdio.h>
int main() {
// Declare a variable of type Student
struct Student student1;
return 0;
}
In this example:
We define a structure called Student with members name , age , and gpa .
Output:
Student Details:
Name: Alice
Age: 20
GPA: 3.80
5. Example of Structure in C
#include <stdio.h>
int main() {
// Declare a variable of type Car
struct Car myCar;
return 0;
}
In this example:
We define a structure called Car with members make , model , year , and price .
We fill in the details (make, model, year, and price) for myCar .
Output:
Car Details:
Make: Toyota
Model: Corolla
Year: 2020
Price: $20000.00
6. Example of Structure
#include <stdio.h>
int main() {
// Declare a variable of type Point3D
struct Point3D point1;
return 0;
}
In this example:
We define a structure called Point3D with members x , y , and z representing coordinates
in 3D space.
Output:
Point coordinates:
x: 1.00
y: 2.50
z: -3.70
7. Example of Structure
Certainly! Let's consider a structure representing a simple employee with their name, employee
ID, and salary.
#include <stdio.h>
#include <string.h>
int main() {
// Declare a variable of type Employee
struct Employee emp1;
return 0;
}
In this example:
We define a structure called Employee with members name , employeeID , and salary .
Output:
Employee Details:
Name: John Doe
Employee ID: 1001
Salary: $50000.00
8. Example of Structure
#include <stdio.h>
int main() {
// Declare a variable of type Computer
struct Computer myComputer;
return 0;
}
In this example:
We define a structure called Computer with members brand , model , year , and price .
We fill in the details (brand, model, year, and price) for myComputer .
Computer Details:
Brand: Dell
Model: XPS 13
Year: 2021
Price: $1200.00
9. Example of Structure
#include <stdio.h>
int main() {
// Declare a variable of type BankAccount
struct BankAccount account1;
return 0;
}
In this example:
We fill in the details (account number, account holder name, and balance) for account1 .
Output:
Bank Account Details:
Account Number: 123456789
Account Holder: Alice Smith
Balance: $1500.00
#include <stdio.h>
int main() {
// Declare a variable of type Rectangle
struct Rectangle rect1;
return 0;
}
In this example:
Output:
Rectangle Details:
Length: 5.00 units
Width: 3.00 units
Area: 15.00 square units
Array of Structures
An array of structures in C is an arrangement where each element of the array is a structure. This
allows you to group multiple instances of a structure together, creating a collection of related
data.
#include <stdio.h>
#include <string.h>
#define MAX_STUDENTS 3
int main() {
// Declare an array of Student structures
struct Student students[MAX_STUDENTS];
strcpy(students[1].name, "Bob");
students[1].id = 1002;
strcpy(students[2].name, "Charlie");
students[2].id = 1003;
return 0;
}
In this example:
We fill in the details (name and id) for each student in the array.
Student Details:
Student 1:
Name: Alice
ID: 1001
Student 2:
Name: Bob
ID: 1002
Student 3:
Name: Charlie
ID: 1003
#include <stdio.h>
#include <string.h>
int main() {
// Declare an array of students
struct Student students[3];
strcpy(students[1].name, "Bob");
students[1].age = 21;
students[1].gpa = 3.5;
strcpy(students[2].name, "Charlie");
students[2].age = 19;
students[2].gpa = 3.9;
In this example:
We define a structure called Student with members name , age , and gpa .
Output:
Student Details:
Student 1
Name: Alice
Age: 20
GPA: 3.70
Student 2
Name: Bob
Age: 21
GPA: 3.50
Student 3
Name: Charlie
Age: 19
GPA: 3.90
#include <stdio.h>
#include <string.h>
int main() {
// Define an array of structures to hold student information
struct Student students[3]; // Array of 3 students
strcpy(students[2].name, "Charlie");
students[2].rollNumber = 103;
students[2].marks = 92.2;
return 0;
}
In this example:
We define a structure called Student with members name , rollNumber , and marks .
We declare an array students of type Student to hold information for multiple students.
We fill in the details (name, roll number, and marks) for each student in the array.
We then display the details of each student in the array using a loop.
Output:
Student 1 Details:
Name: Alice
Roll Number: 101
Marks: 85.50
Student 2 Details:
Name: Bob
Roll Number: 102
Marks: 78.00
Student 3 Details:
Name: Charlie
Roll Number: 103
Marks: 92.20
Sure! Let's extend the previous example to demonstrate an array of structures representing
multiple rectangles:
#include <stdio.h>
int main() {
// Define an array of rectangles
struct Rectangle rectangles[3];
rectangles[1].length = 7.0;
rectangles[1].width = 4.0;
rectangles[2].length = 4.5;
rectangles[2].width = 2.5;
return 0;
}
In this example:
Output:
Rectangle 1 Details:
Length: 5.00 units
Width: 3.00 units
Area: 15.00 square units
Rectangle 2 Details:
Length: 7.00 units
Width: 4.00 units
Area: 28.00 square units
Rectangle 3 Details:
Length: 4.50 units
Width: 2.50 units
Area: 11.25 square units
#include <stdio.h>
#include <string.h>
int main() {
// Declare an array of Employee structures
struct Employee employees[3];
return 0;
}
In this example:
We define a structure called Employee with members name , employeeID , and salary .
We fill in the details (name, employee ID, and salary) for each employee.
Employee 1 Details:
Name: John Doe
Employee ID: 1001
Salary: $60000.00
Employee 2 Details:
Name: Alice Smith
Employee ID: 1002
Salary: $55000.00
Employee 3 Details:
Name: Bob Johnson
Employee ID: 1003
Salary: $58000.00
#include <stdio.h>
#include <string.h>
int main() {
// Declare an array of students
struct Student students[3]; // Assuming we have 3 students
strcpy(students[1].name, "Bob");
students[1].age = 21;
strcpy(students[2].name, "Charlie");
students[2].age = 19;
In this example:
We fill in the details (name and age) for each student in the array.
Output:
Student Details:
Student 1
Name: Alice
Age: 20
Student 2
Name: Bob
Age: 21
Student 3
Name: Charlie
Age: 19
Problem Statement:
You are asked to create a program that manages information about books. Each book has the
following attributes:
Title (a string)
Author (a string)
ISBN (a string)
Price (a float)
3. Write a function to input data for a book (Title, Author, ISBN, and Price) from the user.
#include <stdio.h>
#include <string.h>
return 0;
}
Practice Question:
1. Let's create a program that defines a structure to represent a point in 2D space (x, y
coordinates). The program will then calculate the distance between two points.
Have a function calculateDistance that calculates the distance between two points using
the distance formula: $$ { \sqrt{(x{2}-x{1})^2+(y_{2}-y{1})^2} } $$
In the main function, create two points and calculate the distance between them using the
calculateDistance function.