C Structures
1. What is a Structure?
-
A structure is a user-defined data type in C.
-
It allows you to store multiple variables of different data types under a single name.
-
Useful for representing complex objects like a student, employee, or point in 2D space.
Syntax:
2. Example: Defining a Structure
Output:
Use
strcpy()from<string.h>to assign string values tochararrays.
3. Declaring Structure Variables
-
You can also declare and define together using
typedef(simpler):
4. Accessing Structure Members
-
Use the dot
.operator for normal variables:
-
For pointers to structures, use arrow
->operator:
5. Example: Structure Pointer
Output:
6. Nested Structures
-
Structures can contain other structures:
Output:
7. Array of Structures
Output:
8. Key Points About Structures
-
Structures group different data types into a single entity.
-
Access members using dot
.(normal) or arrow->(pointer). -
Use
typedefto simplify structure names. -
Can have nested structures and arrays of structures.
-
Structures are commonly used in records, objects, and complex data handling.
