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

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

2021/2022

Uploaded on 09/27/2022

judyth
judyth 🇺🇸

4.6

(27)

316 documents

1 / 95

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
2D Array, Struct, Malloc
Shuai Mu
based on slides from Tiger Wang and
Jinyang Li
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36
pf37
pf38
pf39
pf3a
pf3b
pf3c
pf3d
pf3e
pf3f
pf40
pf41
pf42
pf43
pf44
pf45
pf46
pf47
pf48
pf49
pf4a
pf4b
pf4c
pf4d
pf4e
pf4f
pf50
pf51
pf52
pf53
pf54
pf55
pf56
pf57
pf58
pf59
pf5a
pf5b
pf5c
pf5d
pf5e
pf5f

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