Unit 4: Structure
Introduction to Structures
A structure is a collection of variables of different data types grouped together under one name.
Defined using the keyword struct.
Defining and Declaring a Structure
A. Structure Definition
Syntax:
Example:
This only defines the structure — it does not create memory.
B. Declaring Structure Variables
Method 1: After structure definition
Method 2: Inside structure (recommended)
Initializing a Structure
A. Direct initialization
B. Assign each value separately
Accessing Structure Members
Use the dot operator (.) for accessing members.
Example:
Operations on Individual Members
You can perform all normal operations on structure members.
Example:
Members behave like normal variables.
Operations on Structures
Allowed:
✔ Assign one structure variable to another (same type)
✔ Passing structure to a function
✔ Returning a structure
✔ Structure pointers
Not Allowed:
❌ Direct comparison (==, !=)
❌ Arithmetic on entire structures
Structure Within Structure (Nested Structure)
A structure can contain another structure as a member.
Example:
Accessing:
Array of Structures
Used when you want to store multiple data records, e.g., list of students.
Example:
Input:
Accessing:
Pointer to Structure
You can create pointers to structures and access members using arrow operator (->).
Example:
Arrow vs Dot
s1.roll→ dot for normal variableptr->roll→ arrow for pointer
Passing Structures to Functions
A. Pass by value
B. Pass by pointer (preferred)
Example: Full Program Using Structure
Summary Table
| Concept | Meaning |
|---|---|
| Structure | User-defined datatype |
| Access operator | Dot . |
| Nested structure | Structure inside structure |
| Array of structure | Multiple items of same struct |
| Pointer to structure | Uses -> |
| Structure initialization | Using { } or assignment |
| Allowed operations | Assignment, function call |
| Not allowed | Structure comparison & arithmetic |
Union in C
A union in C is a special data type that allows you to store different data types in the same memory location.
- It is similar to a structure.
- BUT in structure each member has its own memory.
- In union, all members share the same memory.
This means only one member can store a value at a time.
Declaring a Union
Syntax:
Example:
Usage of Unions
You create a union variable just like a structure variable.
Example:
Then assign values:
Because they share the same memory, storing something new replaces the previous.
Operations on Union
You can access the members using dot (.) operator:
⚠ Only the last stored member will hold the correct value.
Enumerated Data Types (enum)
An enum (enumeration) allows you to create a list of named integer constants.
- Makes the code easier to read and understand.
- Default values start from 0, 1, 2, … automatically.
Declaring Enum
Syntax:
Example:
Here:
- Monday = 0
- Tuesday = 1
- Wednesday = 2 ... and so on.
Using enum Variable
Assign Custom Values
Difference Between Structure and Union (Quick Table)
| Feature | Structure | Union |
|---|---|---|
| Memory | Each member has separate memory | All members share the same memory |
| Size | Sum of all members | Size of the largest member |
| Simultaneous storage | Can store all values at once | Only one member at a time |
| Use Case | Multiple data needed together | Efficient memory usage |
Storage Classes in C
A storage class in C defines four things about a variable:
- Lifetime – how long the variable exists in memory
- Scope – where the variable can be accessed
- Default value
- Memory location
C provides 4 storage classes:
- auto (Automatic)
- register
- static
- extern (External)
Automatic Storage Class (auto)
- Local variables inside a function are automatic by default.
- You don’t need to write
autokeyword (though it exists).
Features
| Feature | Description |
|---|---|
| Scope | Local to the function/block |
| Lifetime | During function execution |
| Default Value | Garbage value |
| Memory | RAM |
Example
Register Storage Class (register)
-
Suggests the compiler to store the variable in CPU registers (faster access).
Features
| Feature | Description |
|---|---|
| Scope | Local to the function/block |
| Lifetime | During function execution |
| Default Value | Garbage |
| Memory | CPU register (if available) |
Example
⚠ You cannot use & operator on register variables
Static Storage Class (static)
- Static variables retain their value throughout the program execution.
- Lifetime = whole program.
Features
| Feature | Description |
|---|---|
| Scope | Local but value is retained between function calls |
| Lifetime | Entire program execution |
| Default Value | 0 |
| Memory | Static memory (global area) |
Example
If you call fun() 3 times, output will be:
(Unlike auto which resets every time)
External Storage Class (extern)
-
Used to declare a global variable that is already defined somewhere else.
Features
| Feature | Description |
|---|---|
| Scope | Global (accessible across multiple files) |
| Lifetime | Entire program execution |
| Default Value | 0 |
| Memory | Global memory |
Example
File 1: int x = 10; // global definition
File 2: extern int x; // declaration
⚠ extern does not allocate memory, it only refers to an existing variable.
Summary Table: Storage Classes
| Storage Class | Keyword | Scope | Lifetime | Default Value | Memory |
|---|---|---|---|---|---|
| Automatic | auto | Local | Function block only | Garbage | RAM |
| Register | register | Local | Function block only | Garbage | CPU Register |
| Static | static | Local/Global | Entire program | 0 | Static area |
| External | extern | Global | Entire program | 0 | Global memory |