Unit 4: Structures, pointers and file handling
Problem Solving Techniques notes · PTU syllabus (UGSEC2501)
On this page
Unit summary
This unit covers C's most powerful features. Structures group different kinds of data into one record, pointers work directly with memory addresses, and files let a program store data permanently on disk.
After this unit you can
- Define structures, create arrays of structures and pass structures to functions
- Explain unions and how they differ from structures
- Declare and use pointers, including pointers with arrays and structures
- Open, read, write and close data files
PTU syllabus topics
- Defining and processing a structure
- user-defined data types
- structures and pointers
- passing structures to functions
- unions
- pointer declaration and initialization
- accessing a variable through its pointer
- pointers and arrays
- file operations and processing a data file
Memory
Sum of all members
Size of the largest member
Members in use
All at the same time
One at a time
Keyword
struct
union
Use
Records such as a student
Saving memory when only one value is needed
Topic 1
Structures
A structure groups variables of different data types under one name. Each variable inside it is a member.
cstruct student {
int roll;
char name[20];
float marks;
};
struct student s1 = {1, "Ana", 88.5};
printf("%s scored %.1f", s1.name, s1.marks);- Members are accessed with the dot operator (
s1.roll). - An array of structures stores many records:
struct student cls[60]; - typedef creates a user-defined type name:
typedef struct student Student; - Structures can be passed to functions by value (a copy) or by address (a pointer).
Topic 2
Unions
A union is like a structure, but all members share the same memory, so only one member holds a value at a time. Its size equals the size of its largest member.
Keyword
struct
union
Memory
Each member has its own; size = sum of members (plus padding)
All members share; size = largest member
Values held
All members at once
One member at a time
Use
Records like a student
Saving memory when only one field is needed at a time
Example
A struct with an int (4 bytes) and a float (4 bytes) needs 8 bytes; a union with the same members needs only 4.
Topic 3
Pointers
A pointer is a variable that stores the address of another variable.
int x = 10; int *p = &x;—&xis the address of x;pholds it.*p(dereferencing) gives the value at that address, so*pis 10, and*p = 20;changes x to 20.- Pointers allow call by reference, dynamic memory allocation and efficient array handling.
cvoid swap(int *a, int *b) {
int t = *a;
*a = *b;
*b = t;
}
/* called as swap(&x, &y); */Topic 4
Pointers with arrays and structures
- The name of an array is the address of its first element:
ais the same as&a[0], and*(a + i)isa[i]. - Pointer arithmetic:
p + 1moves to the next element (by the size of the data type). - Pointer to a structure uses the arrow operator:
struct student *ptr = &s1; ptr->marksis the same as(*ptr).marks.
Topic 5
File handling
Data in variables is lost when the program ends; files store it permanently.
- 1Declare a file pointer
FILE *fp;
- 2Open the file
fp = fopen("emp.txt", "w");
- 3Read or write
fprintf, fscanf, fputc, fgetc
- 4Close the file
fclose(fp);
| Mode | Meaning |
|---|---|
| "r" | Read; file must exist |
| "w" | Write; creates a new file or overwrites |
| "a" | Append; adds to the end |
| "r+" | Read and write |
Exam tip
Always check whether fopen returned NULL (file not found or not openable) before reading.
Key terms
- Structure
- A user-defined type grouping members of different types
- Union
- A structure-like type whose members share one memory location
- Pointer
- A variable that stores a memory address
- Dereferencing
- Getting the value at a pointer's address with *
- File pointer
- A FILE * variable used to access a file
Quick revision
- Dot operator for structure variables; arrow (->) for structure pointers.
- Union size = largest member; only one member valid at a time.
- & gives an address; * gives the value at an address.
- a[i] is the same as *(a + i).
- fopen → read/write → fclose; check for NULL.
Important exam questions
Practice questions written to the PTU exam pattern for this unit's syllabus: short answers (Section A style) and long answers (Sections B and C style).
Short-answer questions
- Q1.Define a structure with an example.
- Q2.Differentiate between a structure and a union.
- Q3.What is a pointer? How is it declared?
- Q4.What is the use of the arrow operator?
- Q5.List four file opening modes.
- Q6.What does typedef do?
Long-answer questions
- Q1.Explain structures in C: declaration, initialisation, array of structures and passing structures to functions.
- Q2.Explain pointers with examples and write a program to swap two numbers using pointers.
- Q3.Explain the relationship between pointers and arrays.
- Q4.Write a program to store employee details in a file and read them back, explaining each file function used.
Stuck on this unit?
Message SBS on WhatsApp for help with Problem Solving Techniques, or to ask about studying BCA at Synetic.
