Unit 3: Arrays, structures and unions
Programming in C notes · PTU syllabus (BSIT102/BSBC102)
On this page
Unit summary
Arrays, strings, structures and unions let programs handle collections of related data. This unit covers array declaration, single and multidimensional arrays, memory representation, matrices, strings and string handling functions, structure declaration and member access, structure initialisation, arrays of structures, nested structures and unions.
After this unit you can
- Declare and use one- and multi-dimensional arrays
- Perform matrix operations
- Use strings and string handling functions
- Define structures, arrays of structures, nested structures and unions
PTU syllabus topics
- Array declaration
- single and multidimensional arrays
- memory representation
- matrices
- strings and string handling functions
- structure declaration and member access
- structure initialization
- arrays of structures
- nested structures
- unions
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 like a student
Saving memory for variant data
Topic 1
Arrays: declaration, types and memory representation
An array is a collection of elements of the same type stored in consecutive memory locations under one name. Elements are accessed by an index starting from 0.
- One-dimensional:
int marks[5] = {70, 82, 65, 90, 77};— marks[0] is 70. - Two-dimensional (matrix):
int a[3][3];— a[i][j] is row i, column j. - Passing an array to a function passes the address of its first element, so the function works on the original array:
void display(int a[], int n);
c/* Largest element of an array */
int max = a[0];
for (int i = 1; i < n; i++)
if (a[i] > max) max = a[i];Topic 2
Matrices
c/* Matrix addition and multiplication */
int a[2][2] = {{1, 2}, {3, 4}}, b[2][2] = {{5, 6}, {7, 8}}, c[2][2] = {0};
for (int i = 0; i < 2; i++)
for (int j = 0; j < 2; j++)
for (int k = 0; k < 2; k++)
c[i][j] += a[i][k] * b[k][j]; /* c = {{19, 22}, {43, 50}} */- Row-major storage: element a[i][j] of an m × n array is at base + (i × n + j) × size.
- Common programs: addition, multiplication (columns of A = rows of B), transpose, sum of diagonals, checking symmetry.
Topic 3
Strings and string handling functions
A string is an array of characters ending with the null character '\0'. char name[10] = "SBS"; uses 4 bytes ('S', 'B', 'S', '\0').
| Function (string.h) | Purpose |
|---|---|
| strlen(s) | Length, not counting '\0' |
| strcpy(d, s) | Copies s into d |
| strcat(d, s) | Joins s to the end of d |
| strcmp(a, b) | Compares: 0 if equal |
| strrev(s) | Reverses (compiler-specific) |
Topic 4
Structures: declaration, access, initialisation, arrays and nesting
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 5
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.
Key terms
- Array
- Collection of elements of the same type in contiguous memory
- Row-major order
- Storing a 2-D array row by row
- String
- Character array ending with the null character
- Structure
- User-defined type grouping different data types
- Union
- Type whose members share the same memory
Quick revision
- Declaration, initialisation, indexing; 1-D and 2-D arrays; row-major addresses.
- Matrix addition, multiplication, transpose.
- Strings and strlen, strcpy, strcat, strcmp, strrev.
- Structures, member access, arrays of structures, nesting.
- Unions vs structures.
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.How is a 2-D array stored in memory?
- Q2.State the condition for matrix multiplication.
- Q3.What is the null character?
- Q4.Name four string functions.
- Q5.How are structure members accessed?
- Q6.Distinguish a structure and a union.
Long-answer questions
- Q1.Explain one- and two-dimensional arrays with memory representation.
- Q2.Write a program to multiply two matrices.
- Q3.Explain strings and string handling functions in C.
- Q4.Explain structures, arrays of structures, nested structures and unions.
Stuck on this unit?
Message SBS on WhatsApp for help with Programming in C, or to ask about studying B.Sc IT at Synetic.
