Docsity

2D Array and Structures in C: Memory Layout, Pointers, and Data Alignment, Schemes and Mind Maps of Computer Programming

Partial preview of the text

Download 2D Array and Structures in C: Memory Layout, Pointers, and Data Alignment and more Schemes and Mind Maps Computer Programming in PDF only on Docsity!

2D Array, Struct, Malloc

Shuai Mu

based on slides from Tiger Wang and

Jinyang Li

2D Arrray 2D arrays are stored contiguously in memory in row-major format

Multi-dimensional arrays int arr[n 1 ][n 2 ][n 3 ]…[n k- 1 ][n k ] Declare a k dimensional array

n

i is the length of the ith dimension Example: 2D array int matrix[2][3]

Multi-dimensional arrays int arr[n 1 ][n 2 ][n 3 ]…[n k- 1 ][n k ] Declare a k dimensional array

n

i is the length of the ith dimension Example: 2D array int matrix[2][3] Row 0 Row 1 Col (^0) Col 1 Col 2

Multi-dimensional arrays int arr[n 1 ][n 2 ][n 3 ]…[n k- 1 ][n k ] Declare a k dimensional array

n

i is the length of the ith dimension Example: 2D array Access an element at second row and third column matrix[1][2] = 10 int matrix[2][3] = {{ 1 , 2 , 3}, {4, 5, 6 }};

Memory layout int matrix[2][3] = {{1, 2 , 3}, { 4 , 5, 6}}; for (int i = 0; i < 2; i++) { for (int j = 0; j < 3; j++) { printf(“%p\n”,&matrix[i][j]); } }

Memory layout 6 (^1) 0x 0x 0x 0x10c 0x 0x 5 4 3 2 1 st^ row

0x matrix[0][0] matrix[0][1] matrix[0][2] matrix[1][0] matrix[1][1] matrix[1][2]

Memory layout 6 (^1) 0x 0x 0x 0x10c 0x 0x 5 4 3 2 1 st^ row 2 nd row … (^) ... 0x matrix[0][0] matrix[0][1] matrix[0][2] matrix[1][0] matrix[1][1] matrix[1][2]

Pointers 6 (^1) 0x 0x 0x 0x10c 0x 0x 5 4 3 2 1 st^ row 2 nd row … (^) ... 0x400 matrix: 0x matrix[0]: 0x matrix[1]: 0x10c matrix[0][0] matrix, matrix[0] matrix[0][1] matrix[0][2] matrix[1][0] matrix[1][1] matrix[1][2] matrix[1]

Pointers 6 (^1) 0x 0x 0x 0x10c 0x 0x 5 4 3 2 1 st^ row 2 nd row … (^) ... 0x matrix[1] matrix[0][0] matrix[0][1] matrix[0][2] matrix[1][0] matrix[1][1] matrix[1][2] matrix, matrix[0] How many ways to define a pointer which points to the head of the array?

Pointers 6 (^1) 0x 0x 0x 0x10c 0x 0x 5 4 3 2 1 st^ row 2 nd row … (^) ... 0x matrix[1] matrix[0][0] matrix[0][1] matrix[0][2] matrix[1][0] matrix[1][1] matrix[1][2] matrix, matrix[0] int *p = &matrix[0][0]; int *p = matrix[0]; int *p = (int *)matrix; How to access matrix[1][0] with p?

Pointers 6 (^1) 0x 0x 0x 0x10c 0x 0x 5 4 3 2 1 st^ row 2 nd row … (^) ... 0x matrix[1] matrix[0][0] matrix[0][1] matrix[0][2] matrix[1][0] matrix[1][1] matrix[1][2] matrix[0] int *p = &matrix[0][0]; int *p = matrix[0]; int *p = (int *)matrix; matrix[1][0]: *(p + 3) p[3]

A general question Given a 2D array matrix[m][n] and a pointer p which points to matrix[0][0], how to use p to access matrix[i][j]? address of matrix[i][j]: p + i * n + j

Accessing 2D array using pointer int *p = matrix[0]; // or int *p = (int *)matrix; for (int i = 0; i < 2 *3; i++) { printf(“%d\n”, p[i]); } int matrix[2][3] = {{1, 2 , 3}, { 4 , 5, 6}}; OR for (int i = 0; i < 2; i++) { for (int j = 0; j < 3; j++) { printf(“%d\n”, matrix[ 2 ][3]); } }