A **Struct** is a collection of variables (called "members" or "fields") that can be of different data types. Structures are used to represent real-world entities like a Student, a Product, or a Point in a 2D plane.
To use a structure, you must first define its template using the struct keyword. This tells the compiler what the "package" looks like.
To access or assign values to the members of a structure, we use the **Dot Operator** (.).
You can initialize a struct in a single line, similar to an array, by placing values in curly braces in the same order they were defined.
One of the best features of structs is that you can copy all the data from one struct to another using a simple assignment operator (=), which isn't possible with arrays.
A structure can also contain another structure as one of its members. This is useful for complex data, like a Person having an Address.
| Feature | Array | Structure |
|---|---|---|
| Data Types | Homogeneous (All same) | Heterogeneous (Different) |
| Access Method | Index number [0] | Member name (.id) |
| Assignment | Cannot use = for full array |
Can use = to copy all data |
typedef keyword with structs. This allows you to write Student s1; instead of struct Student s1; every time.