• ¿Documento equivocado? Cámbialo gratis
  • Escrito por estudiantes que aprobaron
  • Inmediatamente disponible después del pago
  • Leer en línea o como PDF
Vender
¿Dónde estudias?
Tu idioma
Document preview thumbnail
Vista previa 2 fuera de 7 páginas
Notas de lectura

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

Document preview thumbnail
Vista previa 2 fuera de 7 páginas

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.

Vista previa del contenido

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: ");

Información del documento

Subido en
29 de noviembre de 2026
Número de páginas
7
Escrito en
2026/2027
Tipo
Notas de lectura
Profesor(es)
General / standard curriculum
Contiene
Todas las clases
$7.99

¿Documento equivocado? Cámbialo gratis Dentro de los 14 días posteriores a la compra y antes de descargarlo, puedes elegir otro documento. Puedes gastar el importe de nuevo.
Escrito por estudiantes que aprobaron
Inmediatamente disponible después del pago
Leer en línea o como PDF

Vendido
0
Seguidores
0
Artículos
8
Última venta
-



Por qué los estudiantes eligen Stuvia

Creado por compañeros estudiantes, verificado por reseñas

Calidad en la que puedes confiar: escrito por estudiantes que aprobaron y evaluado por otros que han usado estos resúmenes.

¿No estás satisfecho? Elige otro documento

¡No te preocupes! Puedes elegir directamente otro documento que se ajuste mejor a lo que buscas.

Paga como quieras, empieza a estudiar al instante

Sin suscripción, sin compromisos. Paga como estés acostumbrado con tarjeta de crédito y descarga tu documento PDF inmediatamente.

Student with book image

“Comprado, descargado y aprobado. Así de fácil puede ser.”

Alisha Student

Preguntas frecuentes