Bifalgorithm post · Computer science one · Contiguous 2-D Arrays
How can pointer manipulation create a contiguous two-dimensional array?
How can pointer manipulation create a contiguous two-dimensional array? (Computer science one · Contiguous 2-D Arrays) — "In the following example, we will cre…
Ideas in this flowchart
- Dynamic representation of a 4 × 3 matrix in one contiguous memory block
- The matrix contains 4 rows and 3 integer elements per row
- Allocate an array of 4 integer pointers for the rows
- Allocate one memory block for 4 × 3 integers
- Initialize the remaining row pointers
- Set each row pointer using the starting address plus an offset of 3 × i
- Access the matrix with two indices
- Assign each element the value 10 × i + j
- The conceptual matrix contains the rows [0 1 2], [10 11 12], [20 21 22], and [30 31 32]
- All matrix elements are stored in one large contiguous memory block
- Printing the row addresses shows increasing hexadecimal addresses
- The address difference between consecutive rows is 0x0c, or 12 bytes
- Each row occupies 3 integers × 4 bytes, which equals 12 bytes