Unit2a
1. Multidimensional Arrays
1.1 Definition
Example:
int m[3][4];
This defines an array containing 3 arrays, each having 4 integers.
1.2 Pointer Representation
m
is the name of the entire array and is treated as a pointer to the first row.m[0]
names the first row and acts as a pointer to its first element.m[0][0]
refers to the first element.
1.3 Pointer Arithmetic Examples
m + 1
,m + 2
: Point to the second and third rows respectively.*m
,*m + 1
: Dereference the first row.*(m + 1)
,*(m + 1) + 1
: Access elements in the second row.
Addressing:
&m[0][0]
- Address of first element.&m[1][0]
,&m[2][0]
: Addresses of the first element in the second and third rows.
2. Indexing Multi-dimensional Arrays
2.1 Array Indexing Mechanics
Address calculation:
General formula:
address = base + index × element_size
Pointer arithmetic automatically accounts for element size.
2.2 For Two-dimensional Arrays
Offset formula for accessing elements:
offset = (row_number × row_length + column_number) × element_size
3. Passing Multi-dimensional Arrays to Functions
3.1 Passing Mechanism
Pass a pointer when sending a 1D array to a function.
For multi-dimensional arrays, the length of inner arrays must be known.
Example Function:
void transpose(double matrix[][3]);
Length of outer array is unused (optional).
4. Multi-level Arrays
4.1 Characteristics
Rectangular Design: Multidimensional arrays must have all rows of the same length.
Difficulty with heap allocation:
Non-trivial to specify dimensions.
Data structure:
Arrays of pointers to arrays (e.g., row pointers).
4.2 Indexing Representation
Accessing data remains similar to multidimensional indexing:
int **p; p
serves as a pointer to the outer array.
4.3 Dynamic Allocation Example
Memory allocation example:
int **M = malloc(ROWS * sizeof(int *));
for (i = 0; i < ROWS; i++) {
M[i] = malloc(COLS * sizeof(int));
}
Alternatively, allocate all rows:
M[0] = malloc(ROWS * COLS * sizeof(int));
for (i = 1; i < ROWS; i++) {
M[i] = &M[0][i * COLS];
}
5. Compilation Process
5.1 Overview of Compilation Stages
Primary components:
Source File:
hello.c
Pre-processor, Compiler, Assembler, Linker components.
5.2 Compilation Steps
Pre-processing:
gcc -E hello.c
Compilation:
gcc -S hello.c
-> Produceshello.s
Assembly:
gcc -c hello.c
-> Produceshello.o
Linking: Combines object files to create an executable (e.g.
a.out
).
6. ISA (Instruction Set Architecture)
6.1 Definition and Function
Bridge between hardware and software.
Defines data representation (e.g., integers, pointers).
Instructions are also structured as data.
7. Number Representation in Computing
7.1 Abstract Number Representation
Manipulation of data, primarily represented as numbers.
7.2 Base-10 Decimal System
Structure:
Each digit represents powers of 10 (weights) based on position.
Example:
86042 = 8*10^4 + 6*10^3 + ...
7.3 General Base-n Notation
Numbers expressed by digits representing powers of base n.
Formula:
x = dkn^k + ... + d0n^0
where digit values range from 0 to n-1.
8. Binary and Other Number Systems
8.1 Base-2 (Binary)
Utilizes only two digits (0 and 1).
Ideal for computer logic and representation.
8.2 Base-16 (Hexadecimal)
Use of digits 0-9 and letters A-F.
More compact than binary, each hex digit represents 4 bits.
8.3 Conversion Tables
Hex to Binary Conversion: | Hex | Binary | |-----|---------| | 0 | 0000 | | 1 | 0001 | | 2 | 0010 | | 3 | 0011 | | ... | ... | | F | 1111 |
9. Special Numeric Values
9.1 Floating Point Representation
Compact notation for large and small numbers via significant and exponent.
9.2 IEEE Floating-Point Standard
Structure includes sign bit, exponent, and significand.
Handles non-finite values such as ±infinity and NaN (not a number).
9.3 Subnormal Numbers
Represent values very close to zero to fill the gap between zero and the smallest positive normalized number.