Unit 3 of 4 · B.Sc IT Sem 1

Unit 3: Arrays, structures and unions

Programming in C notes · PTU syllabus (BSIT102/BSBC102)

4 min read5 topics10 exam questions
On this page
  1. Unit summary
  2. Arrays: declaration, types and memory representation
  3. Matrices
  4. Strings and string handling functions
  5. Structures: declaration, access, initialisation, arrays and nesting
  6. Unions
  7. Key terms
  8. Quick revision
  9. Important questions

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
1

Topic 1

Arrays: declaration, types and memory representation

Arrays are one-dimensional (a list), two-dimensional (a table or matrix) or multi-dimensional. Elements are stored in consecutive locations, and the index starts at 0. 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];

Example

Address calculation: for int a[5] starting at address 1000 with 4-byte integers, a[3] is at 1000 + 3 × 4 = 1012. Printing the sum of int a[5] = {10, 20, 30, 40, 50}; with a loop gives 150.

Exam tip

Address of a[i] = Base address + i × size of each element.

2

Topic 2

Matrices

A matrix is stored as a two-dimensional array and processed with nested loops — the outer loop for rows and the inner loop for columns.

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.

Example

Matrix multiplication by hand: for a = {{1, 2}, {3, 4}} and b = {{5, 6}, {7, 8}}, c[0][0] = 1×5 + 2×7 = 19, c[0][1] = 1×6 + 2×8 = 22, c[1][0] = 3×5 + 4×7 = 43, c[1][1] = 3×6 + 4×8 = 50.

Exam tip

Multiplication needs three nested loops and is possible only when columns of the first matrix equal rows of the second.

3

Topic 3

Strings and string handling functions

The string.h functions most asked in exams are strlen (length), strcpy (copy), strcat (join), strcmp (compare) and strrev (reverse — available in some compilers only). 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)

Example

String functions: with char a[20] = "Hello", b[] = "World";, strlen(a) is 5; after strcat(a, b);, a becomes HelloWorld; strcmp("abc", "abd") returns a negative value because 'c' comes before 'd'.

Exam tip

strcmp returns 0 when the strings are equal — not 1.

4

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).

Example

A student structure: struct student { int roll; char name[20]; float marks; }; struct student s = {1, "Asha", 88.5}; — printf("%s %.1f", s.name, s.marks); prints Asha 88.5. An array struct student cls[60]; stores a whole class.

Exam tip

Use the dot operator (s.marks) with a structure variable and the arrow operator (p->marks) with a pointer to a structure.

5

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.

ComparisonStructure vs union
Structure
Union

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

  1. Q1.How is a 2-D array stored in memory?
  2. Q2.State the condition for matrix multiplication.
  3. Q3.What is the null character?
  4. Q4.Name four string functions.
  5. Q5.How are structure members accessed?
  6. Q6.Distinguish a structure and a union.

Long-answer questions

  1. Q1.Explain one- and two-dimensional arrays with memory representation.
  2. Q2.Write a program to multiply two matrices.
  3. Q3.Explain strings and string handling functions in C.
  4. 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.

WhatsApp us