Applications of Arrays (1A) Young Won Lim 3/15/17

Similar documents
Applications of Arrays (1A) Young Won Lim 2/11/17

Arrays (1A) Young Won Lim 1/27/17

Arrays (1A) Young Won Lim 12/4/17

Pointers (1A) Young Won Lim 11/1/17

Applications of Pointers (1A) Young Won Lim 4/11/18

Applications of Pointers (1A) Young Won Lim 12/26/17

Applications of Pointers (1A) Young Won Lim 3/14/18

Applications of Pointers (1A) Young Won Lim 3/21/18

Applications of Pointers (1A) Young Won Lim 3/31/18

Applications of Pointers (1A) Young Won Lim 2/27/18

Applications of Pointers (1A) Young Won Lim 1/5/18

Applications of Structures (1A) Young Won Lim 12/8/17

Pointers (1A) Young Won Lim 3/5/18

Applications of Pointers (1A) Young Won Lim 4/24/18

Pointers (1A) Young Won Lim 10/18/17

Applications of Structures (1A) Young Won Lim 12/4/17

Pointers (1A) Young Won Lim 10/23/17

Pointers (1A) Young Won Lim 2/6/18

Pointers (1A) Young Won Lim 1/5/18

Pointers (1A) Young Won Lim 1/9/18

Function Overview (1A) Young Won Lim 10/23/17

Pointers (1A) Young Won Lim 2/10/18

Pointers (1A) Young Won Lim 1/22/18

Memory Arrays (4H) Gate Level Design. Young Won Lim 3/15/16

Overview (1A) Young Won Lim 9/25/17

Overview (1A) Young Won Lim 9/14/17

Overview (1A) Young Won Lim 9/9/17

Structures (1A) Young Won Lim 12/4/17

Structures (1A) Young Won Lim 11/8/16

Structure (1A) Young Won Lim 7/30/13

Pointers (1A) Young Won Lim 12/4/17

Pointers (1A) Young Won Lim 1/14/18

Arrays and Strings (2H) Young Won Lim 3/7/18

File (1A) Young Won Lim 11/25/16

Type (1A) Young Won Lim 2/17/18

Memory (1A) Young Won Lim 9/7/17

Number System (1A) Young Won Lim 7/7/10

Arrays. Young W. Lim Mon. Young W. Lim Arrays Mon 1 / 17

Applications of Array Pointers (1A) Young Won Lim 11/22/18

Variables (2D) Young Won Lim 3/28/18

Arrays. Young W. Lim Wed. Young W. Lim Arrays Wed 1 / 19

Pointers (2G) Young Won Lim 3/7/18

Expressions (2E) Young Won Lim 4/9/18

ELF (1A) Young Won Lim 10/22/14

Example 2. Young Won Lim 11/24/17

Example 3 : using a structure array. Young Won Lim 11/25/17

Example 1. Young Won Lim 11/17/17

Example 1 : using 1-d arrays. Young Won Lim 12/13/17

Expressions (2E) Young Won Lim 3/10/18

Example 3. Young Won Lim 11/22/17

Program Structure (2A) Young Won Lim 5/28/18

Program Structure (2A) Young Won Lim 3/8/18

Procedure Calls. Young W. Lim Sat. Young W. Lim Procedure Calls Sat 1 / 27

Binary Search Tree (2A) Young Won Lim 5/17/18

The Complexity of Algorithms (3A) Young Won Lim 4/3/18

Procedure Calls. Young W. Lim Mon. Young W. Lim Procedure Calls Mon 1 / 29

Algorithms Bubble Sort (1B) Young Won Lim 4/5/18

ARM Assembly Exercise (1B) Young Won Lim 7/16/16

Binary Search Tree (3A) Young Won Lim 6/2/18

CS 31: Intro to Systems Arrays, Structs, Strings, and Pointers. Kevin Webb Swarthmore College March 1, 2016

4) C = 96 * B 5) 1 and 3 only 6) 2 and 4 only

A Sudoku Solver Pruning (3A)

Structures (2I) Young Won Lim 4/17/18

Binary Search Tree (3A) Young Won Lim 6/4/18

Binary Search Tree (3A) Young Won Lim 6/6/18

Accessibility (1A) Young Won Lim 8/22/13

Access. Young W. Lim Fri. Young W. Lim Access Fri 1 / 18

Algorithms Overview (1A) Young Won Lim 3/29/18

A Sudoku Solver (1A) Richard Bird Implementation. Young Won Lim 11/15/16

At this time we have all the pieces necessary to allocate memory for an array dynamically. Following our example, we allocate N integers as follows:

Structures (2I) Young Won Lim 3/7/18

Day05 A. Young W. Lim Sat. Young W. Lim Day05 A Sat 1 / 14

Access. Young W. Lim Sat. Young W. Lim Access Sat 1 / 19

ELF (1A) Young Won Lim 3/24/16

Eulerian Cycle (2A) Young Won Lim 4/26/18

Floating Point Stored & operated on in floating point registers Intel GAS Bytes C Single s 4 float Double l 8 double Extended t 10/12/16 long double

Day02 A. Young W. Lim Sat. Young W. Lim Day02 A Sat 1 / 12

Background Operators (1E) Young Won Lim 7/7/18

Class (1A) Young Won Lim 9/8/14

Functions (4A) Young Won Lim 5/8/17

Machine Programming 4: Structured Data

Data Representation and Storage. Some definitions (in C)

Assembly IV: Complex Data Types. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

State Monad (3D) Young Won Lim 9/25/17

Functions (4A) Young Won Lim 3/16/18

C Programming Language: C ADTs, 2d Dynamic Allocation. Math 230 Assembly Language Programming (Computer Organization) Thursday Jan 31, 2008

Algorithms (7A) Young Won Lim 4/10/17

Class (1A) Young Won Lim 11/20/14

You may work with a partner on this quiz; both of you must submit your answers.

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

Algorithms (7A) Young Won Lim 4/18/17

State Monad Example (3H) Young Won Lim 2/16/18

Week 9 Part 1. Kyle Dewey. Tuesday, August 28, 12

Polymorphism Overview (1A) Young Won Lim 2/20/18

Data Representation and Storage

Review of the C Programming Language for Principles of Operating Systems

Side Effects (3B) Young Won Lim 11/20/17

Arrays. CSE 351 Autumn Instructor: Justin Hsia

Machine-Level Prog. IV - Structured Data

Haskell Overview II (2A) Young Won Lim 8/9/16

Transcription:

Copyright (c) 2009-2017 Young W. Lim. Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.2 or any later version published by the Free Software Foundation; with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts. A copy of the license is included in the section entitled "GNU Free Documentation License". Please send corrections (or suggestions) to youngwlim@hotmail.com. This document was produced by using LibreOffice.

2-D Array Definition int c [4][4]; A matrix view row 0 row 1 row 2 row 3 col 0 col 1 col 2 col 3 c [0][0] c [0][1] c [0][2] c [0][3] c [1][0] c [1][1] c [1][2] c [1][3] c [2][0] c [2][1] c [2][2] c [2][3] c [3][0] c [3][1] c [3][2] c [3][3] 3

Accessing 2-D Arrays via pointers int c [4][4]; int *p = c[0] ; 1. recursive pointers 2. linear array pointers c [ i ][ j ] p[ i*4 + j ] (*(c+i))[ j ] *(c[ i ]+ j) *(*(c+i)+ j) int (*p)[4]; int **q; *(p+ i*4 + j ) 4

1-d array names int a [4]; int c [4] [4]; int a [4] ; The value of a is the starting address of a 4 element array a: pointer to the first element int c[4] [4] ; Each value of c[i] is the starting address of a 4 element array c[i]: pointer to the first element 5

2-d array as a matrix int c[4] [4]; row major ordering c[0] c[1] c[2] c[3] col 0 col 1 col 2 col 3 row 0 row 1 row 2 row 3 consider each c[i] as a pointer to the first element of each 4 element array 6

2-d array stored as a linear array row 0 row 1 row 2 row 3 int c [4][4]; c [0][0] col 0 col 1 col 2 col 3 7 c[0] c[1] c[2] c[3] row address c [0][1] c [0][2] c [0][3] c [1][0] c [1][1] c [1][2] c [1][3] c [2][0] c [2][1] c [2][2] c [2][3] c [3][0] c [3][1] c [3][2] c [3][3] row 0 row 1 row 2 row 3

row 0 row 1 row 2 row 3 Row Address and Element Address c[0] c[1] c[2] c[3] row address c [0][0] c [0][1] c [0][2] c [0][3] c [1][0] c [1][1] c [1][2] c [1][3] c [2][0] c [2][1] c [2][2] c [2][3] c [3][0] c [3][1] c [3][2] row 0 row 1 row 2 row 3 element address c[0]+0 c[0]+1 c[0]+2 c[0]+3 c[1]+0 c[1]+1 c[1]+2 c[1]+3 c[2]+0 c[2]+1 c[2]+2 c[2]+3 c[3]+0 c[3]+1 c[3]+2 *(c [0]+0) *(c [0]+1) *(c [0]+2) *(c [0]+3) *(c [1]+0) *(c [1]+1) *(c [1]+2) *(c [1]+3) *(c [2]+0) *(c [2]+1) *(c [2]+2) *(c [2]+3) *(c [3]+0) *(c [3]+1) *(c [3]+2) c [3][3] c[3]+3 *(c [3]+3) 8

Recursive Conversions to Pointers int c [4][4]; c[i] = *(c+i) hold row addresses assumption (c[ i ]) [ j ] array pointer int (*p)[4]; (*(c+i)) [ j ] *(c[ i ]+ j) double pointer int **q; parenthesis required *(*(c+i)+ j) 9

Nested Arrays int c [4][3] ; int c[4] [3] ; c: an array of integer pointers c c[0] c[1] c[2] c[3] c[0]+0 c[0]+1 c[0]+2 c[1]+0 c[1]+1 c[1]+2 c[2]+0 c[2]+1 c[2]+2 c[3]+0 c[3]+1 c[3]+2 *(c [0]+0) *(c [0]+1) *(c [0]+2) *(c [1]+0) *(c [1]+1) *(c [1]+2) *(c [2]+0) *(c [2]+1) *(c [2]+2) *(c [3]+0) *(c [3]+1) *(c [3]+2) The pointer array : not necessarily allocated in the memory 10

Nested Array declared explicitly typedef int row [3] ; c[0]+0 c[0]+1 *(c [0]+0) *(c [0]+1) row c [4] ; c[0]+2 c[1]+0 *(c [0]+2) *(c [1]+0) c[1]+1 *(c [1]+1) int c[4] [3] ; c[1]+2 c[2]+0 c[2]+1 *(c [1]+2) *(c [2]+0) *(c [2]+1) c c[0] c[1] c[2] c[3] c[2]+2 c[3]+0 c[3]+1 c[3]+2 *(c [2]+2) *(c [3]+0) *(c [3]+1) *(c [3]+2) The pointer array : not necessarily allocated in the memory 11

Array Types 3-element array type c c[0] c[0]+0 c[0]+1 *(c [0]+0) *(c [0]+1) p c[1] c[0]+2 c[1]+0 c[1]+1 *(c [0]+2) *(c [1]+0) *(c [1]+1) int (*p) [3] ; c[1]+2 *(c [1]+2) p = c; c[2] c[2]+0 c[2]+1 *(c [2]+0) *(c [2]+1) c[2]+2 *(c [2]+2) c[3] c[3]+0 c[3]+1 *(c [3]+0) *(c [3]+1) c[3]+2 *(c [3]+2) 12

Pointer to Arrays 3-element array type c *(p+0) c[0]+0 *(c [0]+0) p[0] c[0]+1 *(c [0]+1) c[0]+2 *(c [0]+2) p *(p+1) c[1]+0 *(c [1]+0) p[1] c[1]+1 c[1]+2 *(c [1]+1) *(c [1]+2) *(p+2) c[2]+0 *(c [2]+0) p[2] c[2]+1 c[2]+2 *(c [2]+1) *(c [2]+2) *(p+3) c[3]+0 *(c [3]+0) p[3] c[3]+1 c[3]+2 *(c [3]+1) *(c [3]+2) 13

Pointer to Arrays 3-element array type c p *(p+0) *(p+1) *(p+2) *(p+3) c[0]+0 c[0]+1 c[0]+2 c[1]+0 *(c [0]+0) *(c [0]+1) *(c [0]+2) *(c [1]+0) c[1]+1 *(c [1]+1) c[1]+2 *(c [1]+2) c[2]+0 *(c [2]+0) c[2]+1 *(c [2]+1) c[2]+2 *(c [2]+2) c[3]+0 *(c [3]+0) c[3]+1 *(c [3]+1) c[3]+2 *(c [3]+2) 14

Pointer Array int (*p) [3] ; c[0]+0 c[0]+1 *(c [0]+0) *(c [0]+1) p = c; c[0]+2 c[1]+0 *(c [0]+2) *(c [1]+0) c[1]+1 *(c [1]+1) int c[4] [3] ; c[1]+2 c[2]+0 *(c [1]+2) *(c [2]+0) c p p+0 p+1 p+2 p+3 *(p+0) *(p+1) *(p+2) *(p+3) c[2]+1 c[2]+2 c[3]+0 c[3]+1 c[3]+2 *(c [2]+1) *(c [2]+2) *(c [3]+0) *(c [3]+1) *(c [3]+2) The pointer array : not necessarily allocated in the memory 15

Accessing 2-d arrays via recursive pointers c[i] [j] (*(c+i))[j] *(*(c+i)+j) *(c+i) : row address *(*(c+i)+j) : element address first select a row, then a column c[0]+0 = *(c+0)+0 c[0]+1 = *(c+0)+1 c[0]+2 = *(c+0)+2 c[0]+3 = *(c+0)+3 c[1]+0 = *(c+1)+0 c[1]+1 = *(c+1)+1 c[1]+2 = *(c+1)+2 c[1]+3 = *(c+1)+3 c[2]+0 = *(c+2)+0 c[2]+1 = *(c+2)+1 c[2]+2 = *(c+2)+2 c[2]+3 = *(c+2)+3 c[3]+0= *(c+3)+0 c[3]+1= *(c+3)+1 c[3]+2= *(c+3)+2 c[3]+3= *(c+3)+3 c[0][0] c[0][1] c[0][2] c[0][3] c[1][0] c[1][1] c[1][2] c[1][3] c[2][0] c[2][1] c[2][2] c[2][3] c[3][0] c[3][1] c[3][2] c[3][3] 16

A Linear Memory Address 1-d view 2-d view c[0] + 0 = c[0]+0*4+0 c[0] + 1 = c[0]+0*4+1 c[0] + 2 = c[0]+0*4+2 c[0] + 3 = c[0]+0*4+3 c[0] + 4 = c[0]+1*4+0 c[0] + 5 = c[0]+1*4+1 c[0] + 6 = c[0]+1*4+2 c[0] + 7 = c[0]+1*4+3 c[0] + 8 = c[0]+2*4+0 c[0] + 9 = c[0]+2*4+1 c[0]+10 = c[0]+2*4+2 c[0]+11 = c[0]+2*4+3 c[0]+12 = c[0]+3*4+0 c[0]+13 = c[0]+3*4+1 c[0]+14 = c[0]+3*4+2 c[0]+15 = c[0]+3*4+3 c[0]+0 = *(c+0)+0 c[0]+1 = *(c+0)+1 c[0]+2 = *(c+0)+2 c[0]+3 = *(c+0)+3 c[1]+0 = *(c+1)+0 c[1]+1 = *(c+1)+1 c[1]+2 = *(c+1)+2 c[1]+3 = *(c+1)+3 c[2]+0 = *(c+2)+0 c[2]+1 = *(c+2)+1 c[2]+2 = *(c+2)+2 c[2]+3 = *(c+2)+3 c[3]+0= *(c+3)+0 c[3]+1= *(c+3)+1 c[3]+2= *(c+3)+2 c[3]+3= *(c+3)+3 c[0][0] c[0][1] c[0][2] c[0][3] c[1][0] c[1][1] c[1][2] c[1][3] c[2][0] c[2][1] c[2][2] c[2][3] c[3][0] c[3][1] c[3][2] c[3][3] 17

2-d array access via a single pointer int *p = c[0] ; int c [4][4]; p[ i*4 + j ] *(p+ i*4 + j) c[ i ][ j ] *(*(c+i)+ j) *(p +k) i = k / 4; j = k % 4; 18

A linearization of a 2-D array 1-d view p+0 p+1 p+2 p+3 p+4 p+5 p+6 p+7 p+8 p+9 p+10 p+11 p+12 p+13 p+14 p+15 p[0] p[1] p[2] p[3] p[4] p[5] p[6] p[7] p[8] p[9] p[10] p[11] p[12] p[13] p[14] p[15] 2-d view c[0]+0 = *(c+0)+0 c[0]+1 = *(c+0)+1 c[0]+2 = *(c+0)+2 c[0]+3 = *(c+0)+3 c[1]+0 = *(c+1)+0 c[1]+1 = *(c+1)+1 c[1]+2 = *(c+1)+2 c[1]+3 = *(c+1)+3 c[2]+0 = *(c+2)+0 c[2]+1 = *(c+2)+1 c[2]+2 = *(c+2)+2 c[2]+3 = *(c+2)+3 c[3]+0= *(c+3)+0 c[3]+1= *(c+3)+1 c[3]+2= *(c+3)+2 c[3]+3= *(c+3)+3 c[0][0] c[0][1] c[0][2] c[0][3] c[1][0] c[1][1] c[1][2] c[1][3] c[2][0] c[2][1] c[2][2] c[2][3] c[3][0] c[3][1] c[3][2] c[3][3] 19

Static Allocation of a 2-d Array int A [4][3]; A in %eax, i in %edx, j in %ecx sall $2, %ecx ;; j * 4 leal (%edx, %edx, 2), %edx ;; i * 3 leal (%ecx, %edx, 4), %edx ;; j * 4 + i * 12 movl (%eax, %edx), %eax ;; read M[ X A +4(3i +j) ] c[0] c[1] c[2] c[3] The pointer array : not necessarily allocated in the memory c[0]+0 c[0]+1 c[0]+2 c[1]+0 c[1]+1 c[1]+2 c[2]+0 c[2]+1 c[2]+2 c[3]+0 c[3]+1 c[3]+2 *(c [0]+0) *(c [0]+1) *(c [0]+2) *(c [1]+0) *(c [1]+1) *(c [1]+2) *(c [2]+0) *(c [2]+1) *(c [2]+2) *(c [3]+0) *(c [3]+1) *(c [3]+2) 20

The name of a 2-d array int a [4]; int c [4] [4]; 1. the name of the nested array (recursive definition) 2. a double pointer 3. a pointer to an array 21

c: the nested array name int a [4]; int c [4] [4]; int a [4] ; The array name a holds the starting address of the 4 integer element array element type 4 elements int c[4] [4] ; c[0], c[1], c[2], c[3] holds the starting address of the 4 integer element array element type 4 elements The array name c is the name of the nested array 22

c: a double pointer int a [4]; int c [4] [4]; **c *(*(c+i)+j) int a [4] ; (int * ) a a points to an integer data int c[4] [4] ; (int * ) c[4] (int ** ) c c[i] points to an integer data c points to an integer pointer 23

c: a pointer to an array int a [4]; int c [4] [4]; (*c)[0] (*(c+i))[j] int a [4] ; (int []) a a : an array name int c[4] [4] ; (int []) c[4] (int (*)[4]) c c[i] : array names c : the pointer array name 24

Pointer to Arrays 1. pointer to 1-d arrays int (*p) [4]; 2. pointer to 2-d arrays int (*p) [4][4]; 25

Pointer to 1-d arrays int c [4] [4]; int (*p) [4]; c p p = c; c[0] c[1] c[2] c[3] c: an array of integer pointers c[0][0] c[0][1] c[0][2] c[0][3] c[1][0] c[1][1] c[1][2] c[1][3] c[2][0] c[2][1] c[2][2] c[2][3] c[3][0] c[3][1] c[3][2] c[3][3] 26

Pointer to Integer vs. Pointer to Array int *p ; p = A ; int (*q) [4]; q = &A ; p q A q=&a A The array name int A[4] ; int A[4] ; p=a *p The int pointer type The array type (*q)=a (*q)[0] (*q)[1] (*q)[2] (*q)[3] 27

Must point to an array type (array name) int (*q) [4]; q = A ; int (*q) [4]; q = &A ; q q Warning! A q=&a A The array name int A[4] ; int A[4] ; A (*q)[0] (*q)[1] (*q)[2] (*q)[3] (*q)=a (*q)[0] (*q)[1] (*q)[2] (*q)[3] The array type The array type 28

Pointer to 2-d Arrays int c [4][4]; int (*p) [4][4] = &c; int (*q) [4] = &c[0] ; q &c[0] = c *p (*p)[0] p=&c c p c[0] c[1] c[2] c[3] (*p)[0][0] c[0][0] c[0][1] c[0][2] c[0][3] c[1][0] c[1][1] c[1][2] c[1][3] c[2][0] c[2][1] c[2][2] c[2][3] c[3][0] c[3][1] c[3][2] c[3][3] 29

Pointer to 2-d Arrays accessing elements int c [4][4]; int (*p) [4][4] = &c; int (*q) [4] = c ; q &c[0] = c *p p=&c c p (*p)[0] (*p)[1] (*p)[2] (*p)[3] (*p)[0][0] (*p)[0][1] (*p)[0][2] (*p)[0][3] (*p)[1][0] (*p)[1][1] (*p)[1][2] (*p)[1][3] (*p)[2][0] (*p)[2][1] (*p)[2][2] (*p)[2][3] (*p)[3][0] (*p)[3][1] (*p)[3][2] (*p)[3][3] 30

Pointer to 1-d Arrays int c [4][4]; int (*p) [4][4] = &c; int (*q) [4] = c ; &c[0] = c q (*q) c c[0] c[1] c[2] p c[3] (*q)[0] c[0][0] c[0][1] c[0][2] c[0][3] c[1][0] c[1][1] c[1][2] c[1][3] c[2][0] c[2][1] c[2][2] c[2][3] c[3][0] c[3][1] c[3][2] c[3][3] 31

Pointer to 1-d Arrays accessing 2-d elements int c [4][4]; int (*p) [4][4] = &c; int (*q) [4] = c ; &c[0] = c q (*q) c q[0] q[1] q[2] p q[3] (*q)[0] q[0][0] q[0][1] q[0][2] q[0][3] q[1][0] q[1][1] q[1][2] q[1][3] q[2][0] q[2][1] q[2][2] q[2][3] q[3][0] q[3][1] q[3][2] q[3][3] 32

Pointer to 1-d Arrays pointer notation int c [4][4]; int (*p) [4][4] = &c; int (*q) [4] = c ; &c[0] = c (*(q+0)) q[0] (*(q+1)) q[1] (*(q+2)) q[2] (*(q+3)) q[3] (*(q+0))[0] (*(q+0))[1] (*(q+0))[2] (*(q+0))[3] (*(q+1))[0] (*(q+1))[1] (*(q+1))[2] (*(q+1))[3] (*(q+2))[0] (*(q+2))[1] (*(q+2))[2] (*(q+2))[3] (*(q+3))[0] (*(q+3))[1] (*(q+3))[2] (*(q+3))[3] q[0][0] q[0][1] q[0][2] q[0][3] q[1][0] q[1][1] q[1][2] q[1][3] q[2][0] q[2][1] q[2][2] q[2][3] q[3][0] q[3][1] q[3][2] q[3][3] 33

Dynamic Memory Allocation of 2-d Arrays 1. method 1 int ** c ; c = malloc(4 * sizeof (int *)) ; c[0] = malloc(4 * sizeof ) ; c[1] = malloc(4 * sizeof ) ; c[2] = malloc(4 * sizeof ) ; c[3] = malloc(4 * sizeof ) ; 2. method 2 int (*p) [4] ; p = malloc(4 * 4 * sizeof ) ; 3. method 3 int ** c ; int * p ; c = malloc( 4 * sizeof(int *) ) ; p = malloc( 4 * 4 * sizeof ) ; for (i=0; i<m; i++) c[i] = p + i*n; 34

2-d array dynamic allocation : method 1 (a) int ** c ; c = malloc(4 * sizeof (int *)) ; (int **) c (int *) (int *) (int *) (int *) c[0] c[1] c[2] c[3] array of pointers : allocated physically in memory c: an array of integer pointers 35

2-d array dynamic allocation : method 1 (b) c[0] = malloc(4 * sizeof ) ; c[1] = malloc(4 * sizeof ) ; c[2] = malloc(4 * sizeof ) ; c[3] = malloc(4 * sizeof ) ; (int **) c may not be contiguous because of memory alignments (int *) (int *) (int *) (int *) c[0] c[1] c[2] c[3] c: an array of integer pointers 36 c[0][0] c[0][1] c[0][2] c[0][3] c[1][0] c[1][1] c[1][2] c[1][3] c[2][0] c[2][1] c[2][2] c[2][3] c[3][0] c[3][1] c[3][2] c[3][3]

2-d array dynamic allocation : method 2 &p int (*p) [4] ; p = malloc(4 * 4 * sizeof ) ; p utilize pointer addition property Pointer to Arrays : No physical allocation contiguous p[0] p[1] p[2] p[3] 37 p[0][0] p[0][1] p[0][2] p[0][3] p[1][0] p[1][1] p[1][2] p[1][3] p[2][0] p[2][1] p[2][2] p[2][3] p[3][0] p[3][1] p[3][2] p[3][3]

2-d array dynamic allocation : method 3 (a) int ** c ; int * p ; c = malloc( 4 * sizeof(int *) ) ; p = malloc( 4 * 4 * sizeof ) ; (int **) c contiguous (int *) (int *) (int *) (int *) c[0] c[1] c[2] c[3] array of pointers allocated physically in memory 38 c[0][0] c[0][1] c[0][2] c[0][3] c[1][0] c[1][1] c[1][2] c[1][3] c[2][0] c[2][1] c[2][2] c[2][3] c[3][0] c[3][1] c[3][2] c[3][3]

2-d array dynamic allocation : method 3 (b) for (i=0; i<m; i++) c[i] = p + i*n; (int **) manual assignments of pointers c (int *) (int *) (int *) (int *) c[0] c[1] c[2] c[3] c: an array of integer pointers (int *) p c[0][0] c[0][1] c[0][2] c[0][3] c[1][0] c[1][1] c[1][2] c[1][3] c[2][0] c[2][1] c[2][2] c[2][3] c[3][0] c[3][1] c[3][2] c[3][3] 39

Limitations No index Range Checking Array Size must be a constant expression Variable Array Size Arrays cannot be Copied or Compared Aggregate Initialization and Global Arrays Precedence Rule Index Type Must be Integral 40

References [1] Essential C, Nick Parlante [2] Efficient C Programming, Mark A. Weiss [3] C A Reference Manual, Samuel P. Harbison & Guy L. Steele Jr. [4] C Language Express, I. K. Chun [5] https://pdos.csail.mit.edu/6.828/2008/readings/pointers.pdf 41