























































































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
How to work with 2D arrays and structures in C, including their memory layout, how to use pointers to access their elements, and data alignment. It covers topics such as memory layout of 2D arrays, accessing elements using pointers, and the concept of structures and their size.
Typology: Schemes and Mind Maps
1 / 95
This page cannot be seen from the preview
Don't miss anything!
























































































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
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
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
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]); } }