1 of 15

CSO101: Computer Programming

Pointers & Arrays

O.S.L. Bhavana

2 of 15

Passing Arrays via pointers: Recall

  • Array name refers to the address of the first element in the array.
  • For an array declaration ex: “int a[10];”, the value of a, &a[0], &a is same. The value of all these expressions is the address of the first element in the array.

  • Difference between array name and pointer: Pointers may be

re-assigned different values but array name should not be

re-assigned to a different address.

  • Ex: int a,b, *pa=&b, arr[10]={0};
        • pa = &a; // is valid
        • arr = &b; // is invalid

3 of 15

1-D Array and Pointers

int a[3]={2,5,6};

1000

a[0]

2

1004

a[1]

5

1008

a[2]

6

An array name has multiple interpretations

  1. a as an array – is a collection of 3 elements

4 of 15

1-D Array and Pointers

int a[3]={2,5,6};

a = 1000

a[0]

2

1004

a[1]

5

1008

a[2]

6

An array name has multiple interpretations

  1. a as an array – is a collection of 3 elements
  2. a as an address – is the address 1000

5 of 15

1-D Array and Pointers

int a[3]={2,5,6};

a = 1000

a[0]

2

1004

a[1]

5

1008

a[2]

6

An array name has multiple interpretations

  1. a as an array – is a collection of 3 elements
  2. a as an address – is the address 1000
  3. a as a pointer – points to a[0]

(not the entire array)

  • sizeof operator treats a as an array. sizeof(a) = 12
  • Pointer arithmetic treats a as a pointer:

a+1 = address of a + 1 (size of (a[0]))

= address of a + 4

6 of 15

2-D Array Memory Map

int a[3][4]={{2,5,6,7}, {12,15,16,17}, {22,25,26,27}};

1000

2

a[0][0]

1004

5

a[0][1]

1008

6

a[0][2]

1012

7

a[0][3]

1016

12

a[1][0]

1020

15

a[1][1]

1024

16

a[1][2]

1028

17

a[1][3]

1032

22

a[2][0]

1036

25

a[2][1]

1040

26

a[2][2]

1044

27

a[2][1]

For simplicity we view 2D arrays as matrices

with the left dimension specifying no. of rows

right dimension specifies no. of columns

a[0]

2

5

6

7

a[1]

12

15

16

17

a[2]

22

25

26

27

7 of 15

2-D Array Addressing

int a[3][4]={{2,5,6,7}, {12,15,16,17}, {22,25,26,27}};

1000

2

a[0][0]

1004

5

a[0][1]

1008

6

a[0][2]

1012

7

a[0][3]

1016

12

a[1][0]

1020

15

a[1][1]

1024

16

a[1][2]

1028

17

a[1][3]

1032

22

a[2][0]

1036

25

a[2][1]

1040

26

a[2][2]

1044

27

a[2][1]

Address of a[i][j] = address of a[0][0]

+( i * num_cols +j) sizeof_datatype

= address of a[0][0] + +( i * num_cols +j) 4

;

a[0]

2

5

6

7

a[1]

12

15

16

17

a[2]

22

25

26

27

8 of 15

3-D Array Memory Map

int a[2][3][2]={2,5,6,7,12,15,16,17,22,25,26,27};

1000

2

a[0][0][0]

1004

5

a[0][0][1]

1008

6

a[0][1][0]

1012

7

a[0][1][1]

1016

12

a[0][2][0]

1020

15

a[0][2][1]

1024

16

a[1][0][0]

1028

17

a[1][0][1]

1032

22

a[1][1][0]

1036

25

a[1][1][1]

1040

26

a[1][2][0]

1044

27

a[1][2][1]

a[0]

a[1]

9 of 15

3-D Array Memory Map

int a[2][3][2]={2,5,6,7,12,15,16,17,22,25,26,27};

1000

2

a[0][0][0]

1004

5

a[0][0][1]

1008

6

a[0][1][0]

1012

7

a[0][1][1]

1016

12

a[0][2][0]

1020

15

a[0][2][1]

1024

16

a[1][0][0]

1028

17

a[1][0][1]

1032

22

a[1][1][0]

1036

25

a[1][1][1]

1040

26

a[1][2][0]

1044

27

a[1][2][1]

a[0]

a[0][0]

a[0][0][0]=2

a[0][0][1]=5

a[0][1]

a[0][1][0]=6

a[0][1][1]=7

a[0][2]

a[0][2][0]=12

a[0][2][1]=15

a[1]

a[1][0]

a[1][0][0]=16

a[1][0][1]=17

a[1][1]

a[1][1][0]=22

a[1][1][1]=25

a[1][2]

a[1][2][0]=26

a[1][2][1]=27

10 of 15

2-D Array and Pointers

Interpretation of 2-D array:

  1. a as a multi-dimensional array

– is a collection of 3*4 elements

int a[3][4]={{2,5,6,7}, {12,15,16,17}, {22,25,26,27}};

1000

2

a[0][0]

1004

5

a[0][1]

1008

6

a[0][2]

1012

7

a[0][3]

1016

12

a[1][0]

1020

15

a[1][1]

1024

16

a[1][2]

1028

17

a[1][3]

1032

22

a[2][0]

1036

25

a[2][1]

1040

26

a[2][2]

1044

27

a[2][1]

11 of 15

2-D Array and Pointers

int a[3][4]={{2,5,6,7}, {12,15,16,17}, {22,25,26,27}};

a=1000

2

a[0][0]

1004

5

a[0][1]

1008

6

a[0][2]

1012

7

a[0][3]

1016

12

a[1][0]

1020

15

a[1][1]

1024

16

a[1][2]

1028

17

a[1][3]

1032

22

a[2][0]

1036

25

a[2][1]

1040

26

a[2][2]

1044

27

a[2][1]

Interpretation of 2-D array:

  1. a as a multi-dimensional array
  2. a as an address – is the address 1000

12 of 15

2-D Array and Pointers

int a[3][4]={{2,5,6,7}, {12,15,16,17}, {22,25,26,27}};

a=1000

2

a[0][0]

1004

5

a[0][1]

1008

6

a[0][2]

1012

7

a[0][3]

1016

12

a[1][0]

1020

15

a[1][1]

1024

16

a[1][2]

1028

17

a[1][3]

1032

22

a[2][0]

1036

25

a[2][1]

1040

26

a[2][2]

1044

27

a[2][1]

Interpretation of 2-D array:

  1. a as a multi-dimensional array
  2. a as an address
  3. a as a pointer – points to a[0]

of the array. In this case a[0] itself is an array.

Therefore a points to the entire array a[0]

13 of 15

2-D Array and Pointers

int a[3][4]={{2,5,6,7}, {12,15,16,17}, {22,25,26,27}};

a=1000

2

a[0][0]

1004

5

a[0][1]

1008

6

a[0][2]

1012

7

a[0][3]

1016

12

a[1][0]

1020

15

a[1][1]

1024

16

a[1][2]

1028

17

a[1][3]

1032

22

a[2][0]

1036

25

a[2][1]

1040

26

a[2][2]

1044

27

a[2][1]

Interpretation of 2-D array:

  1. a as a multi-dimensional array
  2. a as an address
  3. a as a pointer – points to a[0].

In this case a[0] itself is an array.

Therefore a points to the entire array a[0]

  1. sizeof operator treats a as an array. sizeof(a) = 48
  2. Pointer arithmetic treats a as a pointer:

a+1 = address of a + 1 (size of data pointed by a)

= address of a + sizeof(a[0])

= address of a + 16;

14 of 15

2-D Array and Pointers

int a[3][4]={{2,5,6,7}, {12,15,16,17}, {22,25,26,27}};

a=1000

2

a[0][0]

1004

5

a[0][1]

1008

6

a[0][2]

1012

7

a[0][3]

1016

12

a[1][0]

1020

15

a[1][1]

1024

16

a[1][2]

1028

17

a[1][3]

1032

22

a[2][0]

1036

25

a[2][1]

1040

26

a[2][2]

1044

27

a[2][1]

Interpretation of 2-D array name:

  1. a as a multi-dimensional array
  2. a as an address
  3. a as a pointer – points to a[0]

Recall that a+i points to an int array of 4 elements.

Therefore, the address given by a+0 is 1000,

a+1 is 1016, and a+2 is 1032

15 of 15

3-D Array Addressing

int a[2][3][2]={2,5,6,7,12,15,16,17,22,25,26,27};

1000

2

a[0][0][0]

1004

5

a[0][0][1]

1008

6

a[0][1][0]

1012

7

a[0][1][1]

1016

12

a[0][2][0]

1020

15

a[0][2][1]

1024

16

a[1][0][0]

1028

17

a[1][0][1]

1032

22

a[1][1][0]

1036

25

a[1][1][1]

1040

26

a[1][2][0]

1044

27

a[1][2][1]

For int a[p] [q] [r];

Address of a[i][j] [k]= address of a[0][0]

+( i * q*r +j *r + k) sizeof_datatype

Similar interpretations extend for any general

n-dimensional array