0% found this document useful (0 votes)
131 views

Unit 7 Structure

Structures allow grouping of related data in C. Example structures include a Person with name, age, height members; a Point with x, y coordinates; and an Employee with name, ID, salary members. Variables of the structure type are declared and members initialized and accessed using dot operator.

Uploaded by

sukhmalak5
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
131 views

Unit 7 Structure

Structures allow grouping of related data in C. Example structures include a Person with name, age, height members; a Point with x, y coordinates; and an Employee with name, ID, salary members. Variables of the structure type are declared and members initialized and accessed using dot operator.

Uploaded by

sukhmalak5
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

Structures

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.

Here's a step-by-step guide on how to work with structures in C:

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 .

2. Declaring Structure Variables:


Once you have defined a structure, you can declare variables of that structure type.

struct Person person1; // Declare a variable of type Person


struct Person person2; // Declare another variable of type Person

3. Accessing Structure Members:


To access the members of a structure, you use the dot ( . ) operator followed by the member
name.

strcpy(person1.name, "John Doe");


person1.age = 30;
person1.height = 5.9;

4. Initializing Structures:
You can initialize a structure when declaring it.

struct Person person3 = {"Alice", 25, 5.6};


5. Using Structures with Functions:
Structures can be passed as arguments to functions. You can pass a structure as a value or as a
pointer.

void displayPerson(struct Person p) {


printf("Name: %s\n", p.name);
printf("Age: %d\n", p.age);
printf("Height: %.2f\n", p.height);
}

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.

struct Person people[10]; // Array of 10 Person structures

8. Pointer to Structure:
You can create pointers to structures and access members using the arrow ( -> ) operator.

struct Person *ptrPerson = &person1;


printf("Name: %s\n", ptrPerson->name);
1. Example of structure

Sure, here's a simple example of a structure in C to represent a person:

#include <stdio.h>
#include <string.h>

// Define a structure to represent a person


struct Person {
char name[50];
int age;
float height;
};

int main() {
// Declare a variable of type Person
struct Person person1;

// Fill in the details for person1


strcpy(person1.name, "John Doe");
person1.age = 30;
person1.height = 5.9;

// Display the details of person1


printf("Person 1\n");
printf("Name: %s\n", person1.name);
printf("Age: %d\n", person1.age);
printf("Height: %.2f feet\n", person1.height);

return 0;
}

In this example:

We define a structure called Person with members name , age , and height .

We declare a variable person1 of type Person .

We fill in the details (name, age, and height) for person1 .

We display the details of person1 .

Output:

Person 1
Name: John Doe
Age: 30
Height: 5.90 feet

2. Example of structure

Certainly! Here's another example of a structure in C, representing a point in 2D space:


#include <stdio.h>

// Define a structure to represent a point in 2D space


struct Point {
int x;
int y;
};

int main() {
// Declare a variable of type Point
struct Point p1;

// Fill in the details for the point


p1.x = 10;
p1.y = 20;

// Display the details of the point


printf("Point coordinates: (%d, %d)\n", p1.x, p1.y);

return 0;
}

In this example:

We define a structure called Point with members x and y representing coordinates.

We declare a variable p1 of type Point .

We fill in the coordinates for p1 .

We display the coordinates of p1 .

Output:

Point coordinates: (10, 20)

3. Example of Structure

Certainly! Let's create a structure to represent a book with title, author, and price:

#include <stdio.h>

// Define a structure to represent a book


struct Book {
char title[100];
char author[50];
float price;
};

int main() {
// Declare a variable of type Book
struct Book book1;

// Fill in the details for the book


strcpy(book1.title, "The C Programming Language");
strcpy(book1.author, "Brian W. Kernighan and Dennis M. Ritchie");
book1.price = 29.99;

// Display the details of the book


printf("Book Details:\n");
printf("Title: %s\n", book1.title);
printf("Author: %s\n", book1.author);
printf("Price: $%.2f\n", book1.price);

return 0;
}

In this example:

We define a structure called Book with members title , author , and price .

We declare a variable book1 of type Book .

We fill in the details (title, author, and price) for book1 .

We display the details of book1 .

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

Certainly! Here's another example of a structure in C, representing a student:

#include <stdio.h>

// Define a structure to represent a student


struct Student {
char name[50];
int age;
float gpa;
};

int main() {
// Declare a variable of type Student
struct Student student1;

// Fill in the details for the student


strcpy(student1.name, "Alice");
student1.age = 20;
student1.gpa = 3.8;

// Display the details of the student


printf("Student Details:\n");
printf("Name: %s\n", student1.name);
printf("Age: %d\n", student1.age);
printf("GPA: %.2f\n", student1.gpa);

return 0;
}

In this example:

We define a structure called Student with members name , age , and gpa .

We declare a variable student1 of type Student .

We fill in the details (name, age, and GPA) for student1 .

We display the details of student1 .

Output:

Student Details:
Name: Alice
Age: 20
GPA: 3.80

5. Example of Structure in C

Certainly! Here's another example of a structure in C, representing a car:

#include <stdio.h>

// Define a structure to represent a car


struct Car {
char make[20];
char model[20];
int year;
float price;
};

int main() {
// Declare a variable of type Car
struct Car myCar;

// Fill in the details for the car


strcpy(myCar.make, "Toyota");
strcpy(myCar.model, "Corolla");
myCar.year = 2020;
myCar.price = 20000.00;

// Display the details of the car


printf("Car Details:\n");
printf("Make: %s\n", myCar.make);
printf("Model: %s\n", myCar.model);
printf("Year: %d\n", myCar.year);
printf("Price: $%.2f\n", myCar.price);

return 0;
}

In this example:

We define a structure called Car with members make , model , year , and price .

We declare a variable myCar of type Car .

We fill in the details (make, model, year, and price) for myCar .

We display the details of myCar .

Output:

Car Details:
Make: Toyota
Model: Corolla
Year: 2020
Price: $20000.00

6. Example of Structure

Certainly! Here's an example of a structure in C representing a point in 3D space:

#include <stdio.h>

// Define a structure to represent a point in 3D space


struct Point3D {
float x;
float y;
float z;
};

int main() {
// Declare a variable of type Point3D
struct Point3D point1;

// Fill in the coordinates for the point


point1.x = 1.0;
point1.y = 2.5;
point1.z = -3.7;

// Display the coordinates of the point


printf("Point coordinates:\n");
printf("x: %.2f\n", point1.x);
printf("y: %.2f\n", point1.y);
printf("z: %.2f\n", point1.z);

return 0;
}

In this example:
We define a structure called Point3D with members x , y , and z representing coordinates
in 3D space.

We declare a variable point1 of type Point3D .

We fill in the coordinates for point1 .

We display the coordinates of point1 .

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>

// Define a structure to represent an employee


struct Employee {
char name[50];
int employeeID;
float salary;
};

int main() {
// Declare a variable of type Employee
struct Employee emp1;

// Fill in the details for the employee


strcpy(emp1.name, "John Doe");
emp1.employeeID = 1001;
emp1.salary = 50000.0;

// Display the details of the employee


printf("Employee Details:\n");
printf("Name: %s\n", emp1.name);
printf("Employee ID: %d\n", emp1.employeeID);
printf("Salary: $%.2f\n", emp1.salary);

return 0;
}

In this example:

We define a structure called Employee with members name , employeeID , and salary .

We declare a variable emp1 of type Employee .


We fill in the details (name, employee ID, and salary) for emp1 .

We display the details of emp1 .

Output:

Employee Details:
Name: John Doe
Employee ID: 1001
Salary: $50000.00

8. Example of Structure

Certainly! Let's create a structure to represent a computer with its specifications:

#include <stdio.h>

// Define a structure to represent a computer


struct Computer {
char brand[20];
char model[20];
int year;
float price;
};

int main() {
// Declare a variable of type Computer
struct Computer myComputer;

// Fill in the details for the computer


strcpy(myComputer.brand, "Dell");
strcpy(myComputer.model, "XPS 13");
myComputer.year = 2021;
myComputer.price = 1200.00;

// Display the details of the computer


printf("Computer Details:\n");
printf("Brand: %s\n", myComputer.brand);
printf("Model: %s\n", myComputer.model);
printf("Year: %d\n", myComputer.year);
printf("Price: $%.2f\n", myComputer.price);

return 0;
}

In this example:

We define a structure called Computer with members brand , model , year , and price .

We declare a variable myComputer of type Computer .

We fill in the details (brand, model, year, and price) for myComputer .

We display the details of myComputer .


Output:

Computer Details:
Brand: Dell
Model: XPS 13
Year: 2021
Price: $1200.00

9. Example of Structure

Certainly! Here's an example of a structure in C representing a simple bank account:

#include <stdio.h>

// Define a structure to represent a bank account


struct BankAccount {
char accountNumber[15];
char accountHolderName[50];
float balance;
};

int main() {
// Declare a variable of type BankAccount
struct BankAccount account1;

// Fill in the details for the bank account


strcpy(account1.accountNumber, "123456789");
strcpy(account1.accountHolderName, "Alice Smith");
account1.balance = 1500.00;

// Display the details of the bank account


printf("Bank Account Details:\n");
printf("Account Number: %s\n", account1.accountNumber);
printf("Account Holder: %s\n", account1.accountHolderName);
printf("Balance: $%.2f\n", account1.balance);

return 0;
}

In this example:

We define a structure called BankAccount with members accountNumber ,


accountHolderName , and balance .

We declare a variable account1 of type BankAccount .

We fill in the details (account number, account holder name, and balance) for account1 .

We display the details of account1 .

Output:
Bank Account Details:
Account Number: 123456789
Account Holder: Alice Smith
Balance: $1500.00

10. Example of Structure

Certainly! Here's another example of a structure in C representing a rectangle:

#include <stdio.h>

// Define a structure to represent a rectangle


struct Rectangle {
float length;
float width;
};

int main() {
// Declare a variable of type Rectangle
struct Rectangle rect1;

// Fill in the details for the rectangle


rect1.length = 5.0;
rect1.width = 3.0;

// Calculate and display the area of the rectangle


float area = rect1.length * rect1.width;
printf("Rectangle Details:\n");
printf("Length: %.2f units\n", rect1.length);
printf("Width: %.2f units\n", rect1.width);
printf("Area: %.2f square units\n", area);

return 0;
}

In this example:

We define a structure called Rectangle with members length and width .

We declare a variable rect1 of type Rectangle .

We fill in the details (length and width) for rect1 .

We calculate and display the area of the rectangle.

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.

Here's an example of an array of structures representing students:

#include <stdio.h>
#include <string.h>

#define MAX_STUDENTS 3

// Define a structure to represent a student


struct Student {
char name[50];
int id;
};

int main() {
// Declare an array of Student structures
struct Student students[MAX_STUDENTS];

// Fill in the details for each student


strcpy(students[0].name, "Alice");
students[0].id = 1001;

strcpy(students[1].name, "Bob");
students[1].id = 1002;

strcpy(students[2].name, "Charlie");
students[2].id = 1003;

// Display the details of each student


printf("Student Details:\n");
for (int i = 0; i < MAX_STUDENTS; i++) {
printf("Student %d:\n", i + 1);
printf("Name: %s\n", students[i].name);
printf("ID: %d\n", students[i].id);
}

return 0;
}

In this example:

We define a structure called Student with members name and id .

We declare an array students of type Student , allowing for MAX_STUDENTS (here, 3)


students.

We fill in the details (name and id) for each student in the array.

We display the details of each student using a loop.


Output:

Student Details:
Student 1:
Name: Alice
ID: 1001
Student 2:
Name: Bob
ID: 1002
Student 3:
Name: Charlie
ID: 1003

Example 1: Array of Structure

Certainly! Here's an example of an array of structures in C, representing multiple students:

#include <stdio.h>
#include <string.h>

// Define a structure to represent a student


struct Student {
char name[50];
int age;
float gpa;
};

int main() {
// Declare an array of students
struct Student students[3];

// Fill in the details for each student


strcpy(students[0].name, "Alice");
students[0].age = 20;
students[0].gpa = 3.7;

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;

// Display the details of each student


printf("Student Details:\n");
for (int i = 0; i < 3; i++) {
printf("Student %d\n", i + 1);
printf("Name: %s\n", students[i].name);
printf("Age: %d\n", students[i].age);
printf("GPA: %.2f\n", students[i].gpa);
printf("\n");
}
return 0;
}

In this example:

We define a structure called Student with members name , age , and gpa .

We declare an array students of type Student to hold multiple students.

We fill in the details for each student in the array.

We display the details of each student using a loop.

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

2. Example array of structure

Certainly! Here's an example of an array of structures in C, representing multiple students:

#include <stdio.h>
#include <string.h>

// Define a structure to represent a student


struct Student {
char name[50];
int rollNumber;
float marks;
};

int main() {
// Define an array of structures to hold student information
struct Student students[3]; // Array of 3 students

// Fill in the details for each student


strcpy(students[0].name, "Alice");
students[0].rollNumber = 101;
students[0].marks = 85.5;
strcpy(students[1].name, "Bob");
students[1].rollNumber = 102;
students[1].marks = 78.0;

strcpy(students[2].name, "Charlie");
students[2].rollNumber = 103;
students[2].marks = 92.2;

// Display the details of each student


for (int i = 0; i < 3; i++) {
printf("Student %d Details:\n", i + 1);
printf("Name: %s\n", students[i].name);
printf("Roll Number: %d\n", students[i].rollNumber);
printf("Marks: %.2f\n", students[i].marks);
printf("\n");
}

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

3. Example of Array of Structure

Sure! Let's extend the previous example to demonstrate an array of structures representing
multiple rectangles:

#include <stdio.h>

// Define a structure to represent a rectangle


struct Rectangle {
float length;
float width;
};

int main() {
// Define an array of rectangles
struct Rectangle rectangles[3];

// Fill in the details for each rectangle


rectangles[0].length = 5.0;
rectangles[0].width = 3.0;

rectangles[1].length = 7.0;
rectangles[1].width = 4.0;

rectangles[2].length = 4.5;
rectangles[2].width = 2.5;

// Display the details and area of each rectangle


for (int i = 0; i < 3; i++) {
float area = rectangles[i].length * rectangles[i].width;
printf("Rectangle %d Details:\n", i + 1);
printf("Length: %.2f units\n", rectangles[i].length);
printf("Width: %.2f units\n", rectangles[i].width);
printf("Area: %.2f square units\n", area);
printf("\n");
}

return 0;
}

In this example:

We define a structure called Rectangle with members length and width .

We declare an array of type Rectangle named rectangles to store multiple rectangles.

We fill in the details (length and width) for each rectangle.

We calculate and display the area of each rectangle.

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

4. Example of Array of Structure

Sure! Here's an example of an array of structures representing employee details in C:

#include <stdio.h>
#include <string.h>

// Define a structure to represent an employee


struct Employee {
char name[50];
int employeeID;
float salary;
};

int main() {
// Declare an array of Employee structures
struct Employee employees[3];

// Fill in the details for each employee


strcpy(employees[0].name, "John Doe");
employees[0].employeeID = 1001;
employees[0].salary = 60000.0;

strcpy(employees[1].name, "Alice Smith");


employees[1].employeeID = 1002;
employees[1].salary = 55000.0;

strcpy(employees[2].name, "Bob Johnson");


employees[2].employeeID = 1003;
employees[2].salary = 58000.0;

// Display the details of each employee


for (int i = 0; i < 3; i++) {
printf("Employee %d Details:\n", i + 1);
printf("Name: %s\n", employees[i].name);
printf("Employee ID: %d\n", employees[i].employeeID);
printf("Salary: $%.2f\n", employees[i].salary);
printf("\n");
}

return 0;
}

In this example:

We define a structure called Employee with members name , employeeID , and salary .

We declare an array of Employee structures to store details for multiple employees.

We fill in the details (name, employee ID, and salary) for each employee.

We use a loop to display the details of each employee.


Output:

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

5. Example of Array of Structure

Certainly! Here's an example of an array of structures in C representing multiple students:

#include <stdio.h>
#include <string.h>

// Define a structure to represent a student


struct Student {
char name[50];
int age;
};

int main() {
// Declare an array of students
struct Student students[3]; // Assuming we have 3 students

// Fill in the details for each student


strcpy(students[0].name, "Alice");
students[0].age = 20;

strcpy(students[1].name, "Bob");
students[1].age = 21;

strcpy(students[2].name, "Charlie");
students[2].age = 19;

// Display the details of each student


printf("Student Details:\n");
for (int i = 0; i < 3; i++) {
printf("Student %d\n", i + 1);
printf("Name: %s\n", students[i].name);
printf("Age: %d\n", students[i].age);
printf("\n");
}
return 0;
}

In this example:

We define a structure called Student with members name and age .

We declare an array of students ( students ) to store multiple student records.

We fill in the details (name and age) for each student in the array.

We display the details of each student.

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)

You need to write a C program that does the following:

1. Define a structure named Book to store the information about a book.

2. Create a variable of type Book to represent a book.

3. Write a function to input data for a book (Title, Author, ISBN, and Price) from the user.

4. Write a function to display the information of the book.

Here's a template to get you started:

#include <stdio.h>
#include <string.h>

// Define the Book structure


struct Book {
char title[100];
char author[100];
char isbn[20];
float price;
};

// Function to input data for a book


void inputBook(struct Book *book) {
printf("Enter Title: ");
// Input code for title
printf("Enter Author: ");
// Input code for author
printf("Enter ISBN: ");
// Input code for ISBN
printf("Enter Price: ");
// Input code for price
}

// Function to display book information


void displayBook(struct Book book) {
printf("Title: %s\n", book.title);
printf("Author: %s\n", book.author);
printf("ISBN: %s\n", book.isbn);
printf("Price: $%.2f\n", book.price);
}
int main() {
struct Book myBook;

// Input data for the book


inputBook(&myBook);

// Display the book information


printf("\nBook Information:\n");
displayBook(myBook);

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.

Consider the following steps:

Define a structure Point to represent a point in 2D space with x and y coordinates.

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.

Finally, display the calculated distance.

You might also like