• Wrong document? Swap it for free
  • Written by students who passed
  • Immediately available after payment
  • Read online or as PDF
Sell
Where do you study
Your language
Document preview thumbnail
Preview 2 out of 7 pages
Class notes

C Programming Comprehensive Lecture Notes: Contiguous Arrays, Multi-Dimensional Matrices, String Processing & Classical Algorithms

Document preview thumbnail
Preview 2 out of 7 pages

Extensive university-grade C Programming Module 04 lecture notes for Computer Science students. Includes formal memory addressing derivations, pointer-array equivalence, buffer overflow prevention (fgets vs gets), step-by-step sorting trace tables (Bubble, Selection, Insertion), matrix multiplication mechanics, and fully solved university examination programs.

Content preview

UNIVERSITY LECTURE SERIES • COMPUTER SCIENCE CORE CURRICULUM


MODULE 04: CONTIGUOUS ARRAYS, MULTI-
DIMENSIONAL MATRICES, C STRINGS &
CLASSICAL ALGORITHMS
Course: B.Sc. / B.Tech. Computer Science | Standard: ISO/IEC 9899 (C99 / C11)




1. Architectural Foundations of Arrays in C
In systems programming, an array is a derived data type representing a fixed-cardinality
sequence of identically typed elements allocated in a single, unbroken block of virtual
memory.
1.1 Memory Addressing & Pointer-Subscript Duality
Because array elements occupy contiguous bytes without inter-element metadata
headers, the CPU computes the exact memory address of any element in O(1) time
using linear offset arithmetic:

Address(A[i]) = Base(A) + i × sizeof(T )


Commutativity of Subscripting: Because A[i] translates directly to *(A + i) , and
pointer addition is commutative ( A + i == i + A ), the expression i[A] is syntactically valid
in standard C and evaluates to the identical element.
Passing Arrays to Functions: When an array is passed as a function argument, only its
starting base address (8 bytes on 64-bit systems) is copied onto the call stack.
Consequently, sizeof(arr) inside the callee returns the size of a pointer ( 8 ), not the
total byte footprint of the array. The array length must always be passed as an explicit
companion parameter.
#include <stdio.h>

void inspect_array(const int arr[], size_t length) {
// sizeof(arr) here equals sizeof(int *) = 8 bytes on 64-bit platforms
printf(“Callee pointer size : %zu bytes\n”, sizeof(arr));
printf(“Array element count : %zu\n”, length);
}

int main(void) {
int dataset[6] = {12, 24, 36, 48, 60, 72};

,printf(“Caller array size : %zu bytes\n”, sizeof(dataset)); // 24 bytes
inspect_array(dataset, sizeof(dataset) / sizeof(dataset[0]));
return 0;
}

2. Multi-Dimensional Arrays & Linearization
Physical computer memory is strictly one-dimensional. A two-dimensional array int
grid[3][4] is organized conceptually as 3 rows by 4 columns, but is mapped onto linear
RAM as an array of 3 contiguous 1D sub-arrays, each containing 4 integers.
2.1 Row-Major Memory Mapping Derivation
Consider int M[3][4] starting at Base Address 0x2000 with sizeof(int) = 4 :
Conceptual 3x4 Matrix: Physical Linear RAM Layout (Row-Major):
Col 0 Col 1 Col 2 Col 3 Row 0: [M[0][0]][M[0][1]][M[0][2]][M[0][3]] -> 0x2000…0x200C
R0 [ 10 20 30 40 ] Row 1: [M[1][0]][M[1][1]][M[1][2]][M[1][3]] -> 0x2010…0x201C
R1 [ 50 60 70 80 ] Row 2: [M[2][0]][M[2][1]][M[2][2]][M[2][3]] -> 0x2020…0x202C
R2 [ 90 100 110 120 ]


Worked Example: To locate M[2][1] (value 100 ):
$$\text{Address}(M[2][1]) = \text{0x2000} + 4 \times [(2 \times 4) + 1] = \text{0x2000}
+ 4 \times 9 = \text{0x2000} + 36_{10} = \text{0x2024}$$


2.2 Cache Locality & Traversal Performance
Traversing a 2D array row-by-row (outer loop on rows i , inner loop on columns j )
accesses sequential memory addresses, maximizing CPU L1/L2 cache line hits (Spatial
Locality). Reversing the loop order causes frequent cache misses on large matrices.

3. String Representation, Safe I/O & Character Processing
C represents strings using the ASCIIZ convention (ASCII characters terminated by a zero
byte ‘\0’ ).
3.1 Input Stream Mechanics: scanf(“%s”) vs. fgets()
scanf(“%s”, buf) : Terminates reading at the first whitespace character (space, tab, or
newline), making it incapable of reading multi-word sentences, and risks buffer overflow
if width specifiers (e.g., %49s ) are omitted.
fgets(buf, sizeof(buf), stdin) : Reads up to sizeof(buf) - 1 characters including spaces,
appends ‘\0’ safely, and retains the trailing ‘\n’ if buffer space permits.
#include <stdio.h>
#include <string.h>

int main(void) {
char line[100];
printf("Enter a full sentence: ");

Document information

Uploaded on
November 29, 2026
Number of pages
7
Written in
2026/2027
Type
Class notes
Professor(s)
General / standard curriculum
Contains
All classes
$7.99

Wrong document? Swap it for free Within 14 days of purchase and before downloading, you can choose a different document. You can simply spend the amount again.
Written by students who passed
Immediately available after payment
Read online or as PDF

Sold
0
Followers
0
Items
8
Last sold
-



Why students choose Stuvia

Created by fellow students, verified by reviews

Quality you can trust: written by students who passed their tests and reviewed by others who've used these notes.

Didn't get what you expected? Choose another document

No worries! You can instantly pick a different document that better fits what you're looking for.

Pay as you like, start learning right away

No subscription, no commitments. Pay the way you're used to via credit card and download your PDF document instantly.

Student with book image

“Bought, downloaded, and aced it. It really can be that simple.”

Alisha Student

Working on your references?

Create accurate citations in APA, MLA and Harvard with our free citation generator.

Working on your references?

Frequently asked questions