Unit2a
Example: int m[3][4];
This defines an array containing 3 arrays, each having 4 integers.
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.
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.
Address calculation:
General formula:
address = base + index × element_size
Pointer arithmetic automatically accounts for element size.
Offset formula for accessing elements:
offset = (row_number × row_length + column_number) × element_size
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).
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).
Accessing data remains similar to multidimensional indexing:
int **p; p
serves as a pointer to the outer array.
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];
}
Primary components:
Source File: hello.c
Pre-processor, Compiler, Assembler, Linker components.
Pre-processing: gcc -E hello.c
Compilation: gcc -S hello.c
-> Produces hello.s
Assembly: gcc -c hello.c
-> Produces hello.o
Linking: Combines object files to create an executable (e.g. a.out
).
Bridge between hardware and software.
Defines data representation (e.g., integers, pointers).
Instructions are also structured as data.
Manipulation of data, primarily represented as numbers.
Structure:
Each digit represents powers of 10 (weights) based on position.
Example: 86042 = 8*10^4 + 6*10^3 + ...
Numbers expressed by digits representing powers of base n.
Formula: x = dkn^k + ... + d0n^0
where digit values range from 0 to n-1.
Utilizes only two digits (0 and 1).
Ideal for computer logic and representation.
Use of digits 0-9 and letters A-F.
More compact than binary, each hex digit represents 4 bits.
Hex to Binary Conversion: | Hex | Binary | |-----|---------| | 0 | 0000 | | 1 | 0001 | | 2 | 0010 | | 3 | 0011 | | ... | ... | | F | 1111 |
Compact notation for large and small numbers via significant and exponent.
Structure includes sign bit, exponent, and significand.
Handles non-finite values such as ±infinity and NaN (not a number).
Represent values very close to zero to fill the gap between zero and the smallest positive normalized number.
Example: int m[3][4];
This defines an array containing 3 arrays, each having 4 integers.
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.
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.
Address calculation:
General formula:
address = base + index × element_size
Pointer arithmetic automatically accounts for element size.
Offset formula for accessing elements:
offset = (row_number × row_length + column_number) × element_size
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).
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).
Accessing data remains similar to multidimensional indexing:
int **p; p
serves as a pointer to the outer array.
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];
}
Primary components:
Source File: hello.c
Pre-processor, Compiler, Assembler, Linker components.
Pre-processing: gcc -E hello.c
Compilation: gcc -S hello.c
-> Produces hello.s
Assembly: gcc -c hello.c
-> Produces hello.o
Linking: Combines object files to create an executable (e.g. a.out
).
Bridge between hardware and software.
Defines data representation (e.g., integers, pointers).
Instructions are also structured as data.
Manipulation of data, primarily represented as numbers.
Structure:
Each digit represents powers of 10 (weights) based on position.
Example: 86042 = 8*10^4 + 6*10^3 + ...
Numbers expressed by digits representing powers of base n.
Formula: x = dkn^k + ... + d0n^0
where digit values range from 0 to n-1.
Utilizes only two digits (0 and 1).
Ideal for computer logic and representation.
Use of digits 0-9 and letters A-F.
More compact than binary, each hex digit represents 4 bits.
Hex to Binary Conversion: | Hex | Binary | |-----|---------| | 0 | 0000 | | 1 | 0001 | | 2 | 0010 | | 3 | 0011 | | ... | ... | | F | 1111 |
Compact notation for large and small numbers via significant and exponent.
Structure includes sign bit, exponent, and significand.
Handles non-finite values such as ±infinity and NaN (not a number).
Represent values very close to zero to fill the gap between zero and the smallest positive normalized number.