applications of pointers (1a) · a pointer to int type pointer to data pointer to int q *q *q **q....

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

Upload: others

Post on 20-May-2020

58 views

Category:

Documents


3 download

TRANSCRIPT

Young Won Lim3/7/18

Applications of Pointers (1A)

Young Won Lim3/7/18

Copyright (c) 2010 - 2018 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 LibreOffice.

Series : 5. Applications of Pointers

3 Young Won Lim3/7/18

Variables and their addresses

&a

data

int a; a

address

int * p;

int **q;

&p p

&q q

Series : 5. Applications of Pointers

4 Young Won Lim3/7/18

Initialization of Variables

&a

data

int a; a

address

int * p = &a;

int **q = &p;

&p p = &a

&q q = &p

Series : 5. Applications of Pointers

5 Young Won Lim3/7/18

Pointed addresses : p, q

p

data

int a; a

address

int * p = &a;

int **q = &p;

q p

&q q

p = &aq = &p

Series : 5. Applications of Pointers

6 Young Won Lim3/7/18

Dereferenced Variables : *p

p

data

int a; *p

address

int * p = &a;

int **q = &p;

&p p

&q q

*p ≡ a

Series : 5. Applications of Pointers

7 Young Won Lim3/7/18

Dereferenced Variables : *p

int a;

int * p = &a;

int **q = &p;

p = &a *p ≡ a

p ≡ &a*(p) ≡ *(&a)* p ≡ a

AddressAssignment

Variables with the same address

Relations after address assignment

Series : 5. Applications of Pointers

8 Young Won Lim3/7/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

Series : 5. Applications of Pointers

9 Young Won Lim3/7/18

Dereferenced Variables : *q, **q

int a;

int * p = &a;

int **q = &p; q = &p *q ≡ p

p = &a *p ≡ a

AddressAssignment

Variables with the same address

q ≡ &p*(q) ≡ *(&p)* q ≡ p**q ≡ *p**q ≡ a

**q ≡ a

Relations after address assignment

Series : 5. Applications of Pointers

10 Young Won Lim3/7/18

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

*q **q

q *q

&q q

**q ≡ a

p *p

&p p

&q q

*p ≡ a

&a a

&p p

&q q

a

Series : 5. Applications of Pointers

11 Young Won Lim3/7/18

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

**q

*p

&a

data

a

address

&p p

&q q

1) Read / Write a2) Read / Write *p3) Read / Write **q

Series : 5. Applications of Pointers

12 Young Won Lim3/7/18

Variables

&a

data int a;

a can hold an integer a

address

&a

a = 100;

a holds an integer 100a 100

dataaddress

Series : 5. Applications of Pointers

13 Young Won Lim3/7/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

Series : 5. Applications of Pointers

14 Young Won Lim3/7/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

Series : 5. Applications of Pointers

15 Young Won Lim3/7/18

Pointer Variables Examples

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

Series : 5. Applications of Pointers

16 Young Won Lim3/7/18

Pointer Variable p

&q 0x3CE q

dataaddress

0x3A00x3AB

= 0x3AB

2000x3A0

p&p

&a adataaddress

p

*p

&p

p

200

p 0x3A0

&p 0x3AB

*p

Series : 5. Applications of Pointers

17 Young Won Lim3/7/18

Pointer Variable q

dataaddress

&q 0x3CE q

dataaddress

0x3A0

*q 0x3A0

0x3AB

q 0x3AB

&q 0x3CE

= 0x3AB

2000x3A0

p

**q 200

0x3A0

&p

&a a

q

*q

**q

&q

q

*q

Series : 5. Applications of Pointers

18 Young Won Lim3/7/18

Interpretation of Pointer (1)

(int)

address(int *)

data

(int **) address

**q

address *q

data

q addresstype

Types Variables

Series : 5. Applications of Pointers

19 Young Won Lim3/7/18

Interpretation of Pointer (2)

**q

address *q

data

q address

**q

address *q

data

q address

q

*q

&q

Variables Addresses

Series : 5. Applications of Pointers

20 Young Won Lim3/7/18

Integer Pointer Examples (1)

int a;

int * p;

int ** q;

a holds an integer

p holds an address

q holds an address

(int)

(int)

(int *)

(int)

of a int pointer type value

of a int type value(int **)

(int *)

Series : 5. Applications of Pointers

21 Young Won Lim3/7/18

Integer Pointer Examples (2)

int a ;

int *p ;

int **q ;

*p

a

*q

**q

q

p

p

q

*q

Series : 5. Applications of Pointers

22 Young Won Lim3/7/18

Integer Pointer Examples (3)

int a ;

int * p ;

int * *q ;

*p

a

*q

**q

q

p

p

q

*q

Series : 5. Applications of Pointers

23 Young Won Lim3/7/18

Integer Pointer Examples (4)

int a ;

int *p ;

int ** q ;

*p

a

*q

**q

q

p

p

q

*q

Series : 5. Applications of Pointers

24 Young Won Lim3/7/18

Integer Pointer Examples (5)

int a = 200;

int * p = &a;

int ** q = &p;

&q q

dataaddress

= &a

= &p

a =200

pi&p

&a

*q = p

*p = a

*q = p = &a

**q = *p = a

int ** q

int * p

int a

types

Series : 5. Applications of Pointers

25 Young Won Lim3/7/18

Integer Pointer Examples (6)

(int)

(int *)

(int)

(int **)

X

(float *)

(float)

X

int a;

int * p;

int ** q;

Series : 5. Applications of Pointers

26 Young Won Lim3/7/18

Variable Declarations

a =100&aint a ;

The variable a holds an integer data

p&pint * p ;

The pointer variable p holds an address,at this address an integer 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

Series : 5. Applications of Pointers

27 Young Won Lim3/7/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

Series : 5. Applications of Pointers

28 Young Won Lim3/7/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

Series : 5. Applications of Pointers

29 Young Won Lim3/7/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

Series : 5. Applications of Pointers

30 Young Won Lim3/7/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

*

*

*

*

* *

Series : 5. Applications of Pointers

31 Young Won Lim3/7/18

Swapping pointers- pass by reference- double pointers

Series : 5. Applications of Pointers

32 Young Won Lim3/7/18

p = &a

q = &b

p = &b

q = &a

Swapping integer pointers

&p

&q

&p

&q

a = 111

b = 222

a = 111

b = 222

Series : 5. Applications of Pointers

33 Young Won Lim3/7/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;

Series : 5. Applications of Pointers

34 Young Won Lim3/7/18

Pass by integer pointer reference

void swap_pointers (int **m, int **n){

int* tmp;

tmp = *m; *m = *n;

*n = tmp;}

int a, b; int *p, *q; p=&a, q=&b;

… swap_pointers( &p, &q );

int ** mint * *m

int ** nint * *n

int * tmp

Series : 5. Applications of Pointers

35 Young Won Lim3/7/18

Array of Pointers

Series : 5. Applications of Pointers

36 Young Won Lim3/7/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

Series : 5. Applications of Pointers

37 Young Won Lim3/7/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]

Series : 5. Applications of Pointers

38 Young Won Lim3/7/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 * *)

Series : 5. Applications of Pointers

39 Young Won Lim3/7/18

2-d Arrays

Series : 5. Applications of Pointers

40 Young Won Lim3/7/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]

Series : 5. Applications of Pointers

41 Young Won Lim3/7/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)

Series : 5. Applications of Pointers

42 Young Won Lim3/7/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)

Series : 5. Applications of Pointers

43 Young Won Lim3/7/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

Series : 5. Applications of Pointers

44 Young Won Lim3/7/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]

Series : 5. Applications of Pointers

45 Young Won Lim3/7/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]

Series : 5. Applications of Pointers

46 Young Won Lim3/7/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 **)

Series : 5. Applications of Pointers

47 Young Won Lim3/7/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));

Series : 5. Applications of Pointers

48 Young Won Lim3/7/18

Pointer to Arrays

Series : 5. Applications of Pointers

49 Young Won Lim3/7/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

Series : 5. Applications of Pointers

50 Young Won Lim3/7/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 (*) [])

Series : 5. Applications of Pointers

51 Young Won Lim3/7/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 ];

Series : 5. Applications of Pointers

52 Young Won Lim3/7/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] =

}

Young Won Lim3/7/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