Structures in C
# CHAPTER 15
Structures in C
1. Introduction
Arrays are great, but they can only store one type of data (e.g., an array of ONLY integers). What if you want to store a student's name (string), age (int), and GPA (float) together? In C, you use a Structure (struct) to create a custom data type that groups related variables of different types under a single name.
2. Learning Objectives
By the end of this chapter, you will be able to:- Define and declare a structure.
-
Access and modify structure members using the dot (
.) operator.
- Create arrays of structures.
-
Use pointers with structures using the arrow (
->) operator.
- Build an Employee Management System.
3. Defining a Structure
You define a structure using thestruct keyword, usually outside the main() function.
*Note: This just creates a blueprint. It doesn't allocate memory until you create a variable of this type.*
4. Declaring and Accessing Structure Variables
Once defined, you create a variable of that structure type and access its members using the dot operator (.).
5. Initializing Structures
You can initialize a structure at the time of declaration, similar to an array.6. Arrays of Structures
If you want to store 100 students, you don't create 100 variables. You create an array of your custom struct.7. Pointers to Structures
When passing a structure to a function, it's more memory-efficient to pass a pointer. When accessing a structure's members via a pointer, you must use the arrow operator (->) instead of the dot operator.
8. Mini Project: Employee Management System
9. Memory-Level Explanation (Struct Padding)
You might thinksizeof(struct Student) equals the exact sum of its parts (50 bytes + 4 bytes + 4 bytes = 58 bytes). However, CPUs read memory in chunks (words). To optimize CPU reading speed, the C compiler adds empty bytes ("padding") between members to align them to 4-byte or 8-byte boundaries. So sizeof might return 60 bytes.
10. Common Mistakes
-
Forgetting the semicolon after the
structdefinition closing brace}.
-
Assigning strings directly:
s1.name = "Alice";is illegal in C. You must usestrcpy(s1.name, "Alice");.
-
Using
.with pointers: Ifptris a pointer,ptr.ageis a syntax error. You must useptr->age.
11. Exercises
-
1.
Define a struct
Pointwithxandyinteger coordinates. Create two points and write a function to calculate the distance between them.
-
2.
Create a struct
Book(title, author, price). Initialize it and print it.
12. MCQ Quiz with Answers
Which keyword is used to create a structure in C?
How do you access a member of a structure variable?
How do you access a member of a structure using a pointer?
Q5. Can a structure contain a function? a) Yes b) No, C structures only contain data (unlike C++ classes) Answer: b) No, C structures only contain data (unlike C++ classes)
What happens if you forget the semicolon at the end of a struct definition?
Is sizeof(struct) always exactly equal to the sum of the sizes of its members?
How do you declare an array of 10 structures named Car?
Which string function is used to assign a string to a structure member?
Structures in C allow you to implement:
13. Interview Questions
- Q: What is structure padding and packing in C?
-
Q: Differentiate between the dot (
.) operator and the arrow (->) operator.
- Q: Can you nest one structure inside another structure? (Yes).
14. Summary
Structures (struct) allow you to group related variables of different data types into a single custom type. You access members using the dot operator, and if accessing via a pointer, you use the arrow operator. Arrays of structures are incredibly useful for managing databases of records.
15. Next Chapter Recommendation
In Chapter 16: Unions and Enumerations, we will look atunion, a memory-saving cousin to struct, and enum, which makes code more readable.