applications of pointers (1a) · series : 5. applications of pointers 15 young won lim 1/2/18...

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

Upload: others

Post on 20-May-2020

3 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Applications of Pointers (1A) · Series : 5. Applications of Pointers 15 Young Won Lim 1/2/18 Pointer to Pointer Variable &q int **q; **q holds an integer q int * *q; *q holds an

Young Won Lim1/2/18

Applications of Pointers (1A)

Page 2: Applications of Pointers (1A) · Series : 5. Applications of Pointers 15 Young Won Lim 1/2/18 Pointer to Pointer Variable &q int **q; **q holds an integer q int * *q; *q holds an

Young Won Lim1/2/18

Copyright (c) 2010 - 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 [email protected].

This document was produced by using OpenOffice.

Page 3: Applications of Pointers (1A) · Series : 5. Applications of Pointers 15 Young Won Lim 1/2/18 Pointer to Pointer Variable &q int **q; **q holds an integer q int * *q; *q holds an

Series : 5. Applications of Pointers

3 Young Won Lim1/2/18

Swapping integer pointers

p = &a

q = &b

&p

&q

p = &b

q = &a

&p

&q

a = 111

b = 222

a = 111

b = 222

Page 4: Applications of Pointers (1A) · Series : 5. Applications of Pointers 15 Young Won Lim 1/2/18 Pointer to Pointer Variable &q int **q; **q holds an integer q int * *q; *q holds an

Series : 5. Applications of Pointers

4 Young Won Lim1/2/18

Swapping integer pointers

p = &a

q = &b

&p

&q

p = &b

q = &a

&p

&q

swap_pointers( &p, &q );

swap_pointers( int **, int ** );

function call

function prototype

int *p, *q;

Page 5: Applications of Pointers (1A) · Series : 5. Applications of Pointers 15 Young Won Lim 1/2/18 Pointer to Pointer Variable &q int **q; **q holds an integer q int * *q; *q holds an

Series : 5. Applications of Pointers

5 Young Won Lim1/2/18

Pass by integer pointer reference

void swap2(int **m, int **n) {int* tmp;

tmp = *m; *m = *n;

*n = tmp;}

int a, b; int *p, *q; …

swap2( &p, &q );

&p p

&q q

Page 6: Applications of Pointers (1A) · Series : 5. Applications of Pointers 15 Young Won Lim 1/2/18 Pointer to Pointer Variable &q int **q; **q holds an integer q int * *q; *q holds an

Series : 5. Applications of Pointers

6 Young Won Lim1/2/18

Variables and their addresses

&a

data

int a; a

address

int * p;

int **q;

&p p

&q q

Page 7: Applications of Pointers (1A) · Series : 5. Applications of Pointers 15 Young Won Lim 1/2/18 Pointer to Pointer Variable &q int **q; **q holds an integer q int * *q; *q holds an

Series : 5. Applications of Pointers

7 Young Won Lim1/2/18

Variables with initializations

&a

data

int a; a

address

int * p = &a;

int **q = &p;

&p p = &a

&q q = &p

Page 8: Applications of Pointers (1A) · Series : 5. Applications of Pointers 15 Young Won Lim 1/2/18 Pointer to Pointer Variable &q int **q; **q holds an integer q int * *q; *q holds an

Series : 5. Applications of Pointers

8 Young Won Lim1/2/18

Pointed addresses : p, q

p

data

int a; a

address

int * p = &a;

int **q = &p;

q p

&q q

Page 9: Applications of Pointers (1A) · Series : 5. Applications of Pointers 15 Young Won Lim 1/2/18 Pointer to Pointer Variable &q int **q; **q holds an integer q int * *q; *q holds an

Series : 5. Applications of Pointers

9 Young Won Lim1/2/18

Dereferenced Variables : *p

p

data

int a; *p

address

int * p = &a;

int **q = &p;

q p

&q q

*p ≡ a

Page 10: Applications of Pointers (1A) · Series : 5. Applications of Pointers 15 Young Won Lim 1/2/18 Pointer to Pointer Variable &q int **q; **q holds an integer q int * *q; *q holds an

Series : 5. Applications of Pointers

10 Young Won Lim1/2/18

Dereferenced Variables : *q, **q

*q

data

int a; **q

address

int * p = &a;

int **q = &p;

q *q

&q q

**q ≡ a

*q ≡ p

Page 11: Applications of Pointers (1A) · Series : 5. Applications of Pointers 15 Young Won Lim 1/2/18 Pointer to Pointer Variable &q int **q; **q holds an integer q int * *q; *q holds an

Series : 5. Applications of Pointers

11 Young Won Lim1/2/18

Two more ways to access a : *p, **q

*q

data

**q

address

q *q

&q q

**q ≡ a

*q ≡ p

p

data

*p

address

q p

&q q

*p ≡ a

&a

data

a

address

&p p

&q q

a

Page 12: Applications of Pointers (1A) · Series : 5. Applications of Pointers 15 Young Won Lim 1/2/18 Pointer to Pointer Variable &q int **q; **q holds an integer q int * *q; *q holds an

Series : 5. Applications of Pointers

12 Young Won Lim1/2/18

Two more ways to access a : *p, **q

**q ≡ a

*q ≡ p

*p ≡ a

&a

data

a

address

&p p

&q q

1) a2) *p3) **q

Page 13: Applications of Pointers (1A) · Series : 5. Applications of Pointers 15 Young Won Lim 1/2/18 Pointer to Pointer Variable &q int **q; **q holds an integer q int * *q; *q holds an

Series : 5. Applications of Pointers

13 Young Won Lim1/2/18

Variables

&a

data int a;

a can hold an integer a

address

&a

a = 100;

a holds an integer 100a 100

dataaddress

Page 14: Applications of Pointers (1A) · Series : 5. Applications of Pointers 15 Young Won Lim 1/2/18 Pointer to Pointer Variable &q int **q; **q holds an integer q int * *q; *q holds an

Series : 5. Applications of Pointers

14 Young Won Lim1/2/18

Pointer Variables

&p

int * p; *p holds an integer

pint * p;

pointer to int

int

int * p;

p holds an address

*p

p holds an addressof a int type data

p

Page 15: Applications of Pointers (1A) · Series : 5. Applications of Pointers 15 Young Won Lim 1/2/18 Pointer to Pointer Variable &q int **q; **q holds an integer q int * *q; *q holds an

Series : 5. Applications of Pointers

15 Young Won Lim1/2/18

Pointer to Pointer Variable

&q

int **q; **q holds an integer

q

int * *q; *q holds an address of a int type variable

pointer to int

int

int ** q;

q holds an address

int ** q; q holds an address of a pointer to int type data pointer to

pointer to int

*qq

*q **q

Page 16: Applications of Pointers (1A) · Series : 5. Applications of Pointers 15 Young Won Lim 1/2/18 Pointer to Pointer Variable &q int **q; **q holds an integer q int * *q; *q holds an

Series : 5. Applications of Pointers

16 Young Won Lim1/2/18

Pointer Variables

int a;

int * p = & a;

int ** q = & p;

&q 0x3CE q

dataaddress

0x3A0

*q 0x3A0

0x3AB

q 0x3AB

&q 0x3CE

= 0x3AB

2000x3A0

p

**q 200

0x3A0

&p

&a a

Page 17: Applications of Pointers (1A) · Series : 5. Applications of Pointers 15 Young Won Lim 1/2/18 Pointer to Pointer Variable &q int **q; **q holds an integer q int * *q; *q holds an

Series : 5. Applications of Pointers

17 Young Won Lim1/2/18

Pointer Variables

&q 0x3CE q

dataaddress

0x3A0

*q 0x3A0

0x3AB

q 0x3AB

&q 0x3CE

= 0x3AB

2000x3A0

p

**q 200

0x3A0

&p

&a a

&q q

dataaddress

*qq

**a a

Page 18: Applications of Pointers (1A) · Series : 5. Applications of Pointers 15 Young Won Lim 1/2/18 Pointer to Pointer Variable &q int **q; **q holds an integer q int * *q; *q holds an

Series : 5. Applications of Pointers

18 Young Won Lim1/2/18

Pointer Variables

&p 0x3CE p

dataaddress

200

int * p;

*p

200

0x3AB

p 0x3AB

&p 0x3CE

= 0x3AB

p

*p

&q 0x3CE q

dataaddress

0x3A0

*q 0x3A0

0x3AB

q 0x3AB

&q 0x3CE

= 0x3AB

2000x3A0

*q

**q 200

0x3A0

q

*q **q

Page 19: Applications of Pointers (1A) · Series : 5. Applications of Pointers 15 Young Won Lim 1/2/18 Pointer to Pointer Variable &q int **q; **q holds an integer q int * *q; *q holds an

Series : 5. Applications of Pointers

19 Young Won Lim1/2/18

Pointer to Pointer Variable

&q 0x3CE

int **q; **q holds an integer

q

dataaddress

0x3A0

int * *q; *q holds an address of

a int type variable pointer to int

int

int ** q;

q holds an address

*q 0x3A0

0x3AB

q 0x3AB

&q 0x3CE

= 0x3AB

int ** q; q holds an address of a pointer to int type data pointer to

pointer to int

2000x3A0

*q

**q 200

0x3A0

q

*q **q

Page 20: Applications of Pointers (1A) · Series : 5. Applications of Pointers 15 Young Won Lim 1/2/18 Pointer to Pointer Variable &q int **q; **q holds an integer q int * *q; *q holds an

Series : 5. Applications of Pointers

20 Young Won Lim1/2/18

Interpretation of Pointer (1)

(int)

address(int *)

data

(int **) address(int *) p

(int) *p

(int **) q

(int *) *q

data

address

address

address

p

q

q *q

*q **q

Page 21: Applications of Pointers (1A) · Series : 5. Applications of Pointers 15 Young Won Lim 1/2/18 Pointer to Pointer Variable &q int **q; **q holds an integer q int * *q; *q holds an

Series : 5. Applications of Pointers

21 Young Won Lim1/2/18

Interpretation of Pointer (2)

p

content of az pointer :Dereferencing operator *

*p

p

The address of a variable :Address of operator &

&

p

& p

*

p

*p

*

Page 22: Applications of Pointers (1A) · Series : 5. Applications of Pointers 15 Young Won Lim 1/2/18 Pointer to Pointer Variable &q int **q; **q holds an integer q int * *q; *q holds an

Series : 5. Applications of Pointers

22 Young Won Lim1/2/18

Integer Pointer Examples (1)

int i;

int * pi;

int ** qi;

i holds an integers

pi holds an address

qi holds an address

int type

int * type

int ** type

(int) *pi

(int) i

(int *) *qi

(int) **qi

of a pointer to int type data

of a int type

int * type

int * type

(int **) qi

(int *) pi

pi

qi

*qi

Page 23: Applications of Pointers (1A) · Series : 5. Applications of Pointers 15 Young Won Lim 1/2/18 Pointer to Pointer Variable &q int **q; **q holds an integer q int * *q; *q holds an

Series : 5. Applications of Pointers

23 Young Won Lim1/2/18

Integer Pointer Examples (2)

int i;

int * pi;

int ** qi;

i holds an integers

pi holds an address

qi holds an address

(int)

(int *)

(int)

of Pointer to Int type

of Int type

(int **) qi

X

(float *)

(float)

X

Page 24: Applications of Pointers (1A) · Series : 5. Applications of Pointers 15 Young Won Lim 1/2/18 Pointer to Pointer Variable &q int **q; **q holds an integer q int * *q; *q holds an

Series : 5. Applications of Pointers

24 Young Won Lim1/2/18

Integer Pointer Examples (3)

int i = 200;

int * pi = &i;

int ** qi = π

&qi qi

dataaddress

= &i

= &pi

i =200

pi&pi

&i

*qi = pi

*pi = i

*qi = pi = &i

**qi = *pi = i

i holds an integers

pi holds an address

qi holds an address

of a pointer to Int type data

of a int type

int ** qi

int * pi

int i

types

Page 25: Applications of Pointers (1A) · Series : 5. Applications of Pointers 15 Young Won Lim 1/2/18 Pointer to Pointer Variable &q int **q; **q holds an integer q int * *q; *q holds an

Series : 5. Applications of Pointers

25 Young Won Lim1/2/18

Variable Declarations

a =100&aint a ;

The variable a holds an integer data

p&pint * p ;

The pointer variable p holds an address,where an integer data is stored

200

q&qint * * q ;

The pointer variable q holds an address,where another address is stored, where an integer data is stored

30

Page 26: Applications of Pointers (1A) · Series : 5. Applications of Pointers 15 Young Won Lim 1/2/18 Pointer to Pointer Variable &q int **q; **q holds an integer q int * *q; *q holds an

Series : 5. Applications of Pointers

26 Young Won Lim1/2/18

Access Data Via Pointer Variables (1)

a =100&aint a ;

p&pint * p ; *p=200

&avalue

a address

&pvalue

paddress

p *p

integer

address

integer

Indirect Access

Direct Access

Dereference Operator *

the content of the pointed location

*(&p) *p

p

Page 27: Applications of Pointers (1A) · Series : 5. Applications of Pointers 15 Young Won Lim 1/2/18 Pointer to Pointer Variable &q int **q; **q holds an integer q int * *q; *q holds an

Series : 5. Applications of Pointers

27 Young Won Lim1/2/18

Access Data Via Pointer Variables (2)

q&qint * * q ; *q **q=30

&qvalue

qaddress

q *q

*q **q

address

integer

addressDouble Indirect Access

Dereference Operator *

the content of the pointed location

*(&q) *q

Dereference Operator *

the content of the pointed location

*q **q

q *q

Page 28: Applications of Pointers (1A) · Series : 5. Applications of Pointers 15 Young Won Lim 1/2/18 Pointer to Pointer Variable &q int **q; **q holds an integer q int * *q; *q holds an

Series : 5. Applications of Pointers

28 Young Won Lim1/2/18

Access Data Via Pointer Variables (3)

a =100&aint a ;

int * p ;

int * * q ;

&avalue

a address

&pvalue

paddress

p *p

&qvalue

qaddress

q *q*q **q

integer

address

integer

address

integer

address

Indirect Access

Double Indirect Access

Direct Access

Dereference Operator *

the content of the pointed location

Dereference Operator *

the content of the pointed location

p&p *p=200 p

q&q *q **q=30 q *q

Page 29: Applications of Pointers (1A) · Series : 5. Applications of Pointers 15 Young Won Lim 1/2/18 Pointer to Pointer Variable &q int **q; **q holds an integer q int * *q; *q holds an

Series : 5. Applications of Pointers

29 Young Won Lim1/2/18

Access Data Via Pointer Variables (4)

a =100&aint a

int * p

int * * q

p&p *p=200 p

q&q *q **q=30 q *q

*(&a) = a

*(&p) = p *(p) = *p

*(&q) = q *(q) = *q *(*q) = **q

*

*

*

*

* *

Page 30: Applications of Pointers (1A) · Series : 5. Applications of Pointers 15 Young Won Lim 1/2/18 Pointer to Pointer Variable &q int **q; **q holds an integer q int * *q; *q holds an

Series : 5. Applications of Pointers

30 Young Won Lim1/2/18

Array of Pointers

Page 31: Applications of Pointers (1A) · Series : 5. Applications of Pointers 15 Young Won Lim 1/2/18 Pointer to Pointer Variable &q int **q; **q holds an integer q int * *q; *q holds an

Series : 5. Applications of Pointers

31 Young Won Lim1/2/18

Array of Pointers (1)

int a [4];

int

Array name a holds the starting address

No. of elements = 4

int * b [4];

a [4]

Type of each element

int *

Array name b holds the starting address

No. of elements = 4

b [4]

Type of each element

Page 32: Applications of Pointers (1A) · Series : 5. Applications of Pointers 15 Young Won Lim 1/2/18 Pointer to Pointer Variable &q int **q; **q holds an integer q int * *q; *q holds an

Series : 5. Applications of Pointers

32 Young Won Lim1/2/18

Array of Pointers (2)

int a [4]; int * b [4];

a[0] a[1]

a[2] a[3]

b[0] b[1]

b[2] b[3]

a b

b[0]

b[1]

b[2]

b[3]

*b[0]

*b[1]

*b[2]

*b[3]

Page 33: Applications of Pointers (1A) · Series : 5. Applications of Pointers 15 Young Won Lim 1/2/18 Pointer to Pointer Variable &q int **q; **q holds an integer q int * *q; *q holds an

Series : 5. Applications of Pointers

33 Young Won Lim1/2/18

Array of Pointers (3)

int a [4]; int * b [4];

(int) (int)

(int) (int)

(int *) (int *)

(int *) (int *)

(int)

(int)

(int)

(int)

(int *) (int * *)

Page 34: Applications of Pointers (1A) · Series : 5. Applications of Pointers 15 Young Won Lim 1/2/18 Pointer to Pointer Variable &q int **q; **q holds an integer q int * *q; *q holds an

Series : 5. Applications of Pointers

34 Young Won Lim1/2/18

2-d Arrays

Page 35: Applications of Pointers (1A) · Series : 5. Applications of Pointers 15 Young Won Lim 1/2/18 Pointer to Pointer Variable &q int **q; **q holds an integer q int * *q; *q holds an

Series : 5. Applications of Pointers

35 Young Won Lim1/2/18

c[0] c[1]

c[2] c[3]

A 2-D Array

c

int c[4] [4] int c [4] [4];c[0]

c[1]

c[2]

c[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]

Page 36: Applications of Pointers (1A) · Series : 5. Applications of Pointers 15 Young Won Lim 1/2/18 Pointer to Pointer Variable &q int **q; **q holds an integer q int * *q; *q holds an

Series : 5. Applications of Pointers

36 Young Won Lim1/2/18

(int *) (int *)

(int *) (int *)

A 2-D Array

(int * *)

int c[4] [4] int c [4] [4];c[0]

c[1]

c[2]

c[3]

(int) (int) (int) (int) (int) (int) (int) (int) (int) (int) (int) (int) (int) (int) (int) (int)

Page 37: Applications of Pointers (1A) · Series : 5. Applications of Pointers 15 Young Won Lim 1/2/18 Pointer to Pointer Variable &q int **q; **q holds an integer q int * *q; *q holds an

Series : 5. Applications of Pointers

37 Young Won Lim1/2/18

A 2-D Array via a double pointer

int c[4] [4]

int c [4] [4]; (c [i])[j]

(*(c+i))[j]

*(*(c+i)+j)

(c [I]) = (*(c+i))

(_)[j] = *((_)+j)

Page 38: Applications of Pointers (1A) · Series : 5. Applications of Pointers 15 Young Won Lim 1/2/18 Pointer to Pointer Variable &q int **q; **q holds an integer q int * *q; *q holds an

Series : 5. Applications of Pointers

38 Young Won Lim1/2/18

(int *) (int *)

(int *) (int *)

A 2-D Array

(int * *)

int c [4] [4];c[0]

c[1]

c[2]

c[3]

(int) (int) (int) (int) (int) (int) (int) (int) (int) (int) (int) (int) (int) (int) (int) (int)

(c [i])[j]

(*(c+i))[j]

*(*(c+i)+j)

(c+i)*(c+i)+j

Page 39: Applications of Pointers (1A) · Series : 5. Applications of Pointers 15 Young Won Lim 1/2/18 Pointer to Pointer Variable &q int **q; **q holds an integer q int * *q; *q holds an

Series : 5. Applications of Pointers

39 Young Won Lim1/2/18

A 2-D array via a single pointer

c[0] c[1]

c[2] c[3]

c

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]

int c [4] [4]; c[0]

c[1]

c[2]

c[3]

0=[0*4+0]

1=[0*4+1]

2=[0*4+2]

3=[0*4+3]

4=[1*4+0]

5=[1*4+1]

6=[1*4+2]

7=[1*4+3]

8=[2*4+0]

9=[2*4+1]

10=[2*4+2]

11=[2*4+3]

12=[3*4+0]

13=[3*4+1]

14=[3*4+2]

15=[3*4+3] c[i][j]

Page 40: Applications of Pointers (1A) · Series : 5. Applications of Pointers 15 Young Won Lim 1/2/18 Pointer to Pointer Variable &q int **q; **q holds an integer q int * *q; *q holds an

Series : 5. Applications of Pointers

40 Young Won Lim1/2/18

A 2-D array via a single pointer

c[0] c[1]

c[2] c[3]

c

int c [4] [4]; c[0]

c[1]

c[2]

c[3]

p[0*4+0]

p[0*4+1]

p[0*4+2]

p[0*4+3]

p[1*4+0]

p[1*4+1]

p[1*4+2]

p[1*4+3]

p[2*4+0]

p[2*4+1]

p[2*4+2]

p[2*4+3]

p[3*4+0]

p[3*4+1]

p[3*4+2]

p[3*4+3]

p =c[0];int *

p[i*4+j]c[i][j]

Page 41: Applications of Pointers (1A) · Series : 5. Applications of Pointers 15 Young Won Lim 1/2/18 Pointer to Pointer Variable &q int **q; **q holds an integer q int * *q; *q holds an

Series : 5. Applications of Pointers

41 Young Won Lim1/2/18

2-D Array Dynamic Memory Allocation (1)

int ** d ;

d = (int **) malloc (4 * size of (int *));for (i=0; i<4; ++i) d[i] = (int *) malloc(4 * sizeof(int));

d[0] d[1]

d[2] d[3]

(int *) (int *)

(int *) (int *)

d(int **)

Page 42: Applications of Pointers (1A) · Series : 5. Applications of Pointers 15 Young Won Lim 1/2/18 Pointer to Pointer Variable &q int **q; **q holds an integer q int * *q; *q holds an

Series : 5. Applications of Pointers

42 Young Won Lim1/2/18

d[0] d[1]

d[2] d[3]

(int *) (int *)

(int *) (int *)

2-D Array Dynamic Memory Allocation (2)

(int) (int) (int) (int)

d[0][0] d[0][1] d[0][2] d[0][3]

(int) (int) (int) (int)

d[1][0] d[1][1] d[1][2] d[1][3]

(int) (int) (int) (int)

d[2][0] d[2][1] d[2][2] d[2][3]

(int) (int) (int) (int)

d[3][0] d[3][1] d[3][2] d[3][3]

d(int **)&d

int ** d ;

d = (int **) malloc (4 * size of (int *));for (i=0; i<4; ++i) d[i] = (int *) malloc(4 * sizeof(int));

Page 43: Applications of Pointers (1A) · Series : 5. Applications of Pointers 15 Young Won Lim 1/2/18 Pointer to Pointer Variable &q int **q; **q holds an integer q int * *q; *q holds an

Series : 5. Applications of Pointers

43 Young Won Lim1/2/18

Pointer to Arrays

Page 44: Applications of Pointers (1A) · Series : 5. Applications of Pointers 15 Young Won Lim 1/2/18 Pointer to Pointer Variable &q int **q; **q holds an integer q int * *q; *q holds an

Series : 5. Applications of Pointers

44 Young Won Lim1/2/18

Pointer to array (1)

int a [4];

a[0] a[1] a[2] a[3]

(int) (int) (int) (int)

a(int [])

int (*p) [4]

int a [4]

int func (int a, int b) ; a prototype

int (* fp) (int a, int b) ; a function's type

int m ;

int *n ;

an integer variable

a pointer variable

int * fp (int a, int b) ;

pointer to the array of 4 elements

function pointer

Page 45: Applications of Pointers (1A) · Series : 5. Applications of Pointers 15 Young Won Lim 1/2/18 Pointer to Pointer Variable &q int **q; **q holds an integer q int * *q; *q holds an

Series : 5. Applications of Pointers

45 Young Won Lim1/2/18

Pointer to array (2)

p int (*p) [4] ;

int a [4]

a[0] a[1]

a[2] a[3]

(int)

(int)

(int)

(int)

a(int [])

an array with 4 integer elements

*p = a

(*p) = a

&(*p) = &a

p = &a

sizeof(p)= 4 bytes

sizeof(*p)= 16 bytes

p*p

(int (*) [])

Page 46: Applications of Pointers (1A) · Series : 5. Applications of Pointers 15 Young Won Lim 1/2/18 Pointer to Pointer Variable &q int **q; **q holds an integer q int * *q; *q holds an

Series : 5. Applications of Pointers

46 Young Won Lim1/2/18

Pointer to array (3)

c[0] c[1] c[2] c[3]

(int [])

(int [])

(int [])

(int [])

c(int (*) []) int c[4] [4]

(int)

(int)

(int)

(int)

c[0][0] c[0][1] c[0][2] c[0][3]

(int)

(int)

(int)

(int)

c[1][0] c[1][1] c[1][2] c[1][3]

(int)

(int)

(int)

(int)

c[2][0] c[2][1] c[2][2] c[2][3]

(int)

(int)

(int)

(int)

c[3][0] c[3][1] c[3][2] c[3][3]

int (*p) [4] ;

p&p

a 2-d array with 4 rows and 4 columns

p = c p

*p

*(p+0)

*(p+1)

*(p+2)

*(p+3)

(int (*) [])

(*p) [ i ][ j ];

Page 47: Applications of Pointers (1A) · Series : 5. Applications of Pointers 15 Young Won Lim 1/2/18 Pointer to Pointer Variable &q int **q; **q holds an integer q int * *q; *q holds an

Series : 5. Applications of Pointers

47 Young Won Lim1/2/18

Pointer to array (4)

int c [4][4];int (*p) [4];

p = c;

func(p, ... );

void func(int (*x)[4], ... ){

x[r][c] =

}

void func(int x[ ][4], ... ){

x[r][c] =

}

Page 48: Applications of Pointers (1A) · Series : 5. Applications of Pointers 15 Young Won Lim 1/2/18 Pointer to Pointer Variable &q int **q; **q holds an integer q int * *q; *q holds an

Young Won Lim1/2/18

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