pointers (1a) - wikimedia · pointers 32 young won lim 11/1/17 variable declarations int a ; &a...

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

Upload: others

Post on 20-May-2020

6 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Pointers (1A) - Wikimedia · Pointers 32 Young Won Lim 11/1/17 Variable Declarations int a ; &a a =100 The variable a holds an integer data int * p ; &p p The pointer variable p holds

Young Won Lim11/1/17

Pointers (1A)

Page 2: Pointers (1A) - Wikimedia · Pointers 32 Young Won Lim 11/1/17 Variable Declarations int a ; &a a =100 The variable a holds an integer data int * p ; &p p The pointer variable p holds

Young Won Lim11/1/17

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: Pointers (1A) - Wikimedia · Pointers 32 Young Won Lim 11/1/17 Variable Declarations int a ; &a a =100 The variable a holds an integer data int * p ; &p p The pointer variable p holds

Pointers 3 Young Won Lim11/1/17

Address and Data in a Memory

0x000

1K x 8Memory

address 10 bits

dataaddress

data 8 bits

0x0010x0020x003

0x3FC0x3FD0x3FE0x3FF

210 = 1024

HEXaddress

8 bits10 bits

data

Page 4: Pointers (1A) - Wikimedia · Pointers 32 Young Won Lim 11/1/17 Variable Declarations int a ; &a a =100 The variable a holds an integer data int * p ; &p p The pointer variable p holds

Pointers 4 Young Won Lim11/1/17

Variables and their addresses

&a

data

int a; a

address

int * p;

int **q;

&p p

&q q

Page 5: Pointers (1A) - Wikimedia · Pointers 32 Young Won Lim 11/1/17 Variable Declarations int a ; &a a =100 The variable a holds an integer data int * p ; &p p The pointer variable p holds

Pointers 5 Young Won Lim11/1/17

Variables with initializations

&a

data

int a; a

address

int * p = &a;

int **q = &p;

&p p = &a

&q q = &p

Page 6: Pointers (1A) - Wikimedia · Pointers 32 Young Won Lim 11/1/17 Variable Declarations int a ; &a a =100 The variable a holds an integer data int * p ; &p p The pointer variable p holds

Pointers 6 Young Won Lim11/1/17

Pointed addresses : p, q

p

data

int a; a

address

int * p = &a;

int **q = &p;

q p

&q q

Page 7: Pointers (1A) - Wikimedia · Pointers 32 Young Won Lim 11/1/17 Variable Declarations int a ; &a a =100 The variable a holds an integer data int * p ; &p p The pointer variable p holds

Pointers 7 Young Won Lim11/1/17

Dereferenced Variables : *p

p

data

int a; *p

address

int * p = &a;

int **q = &p;

q p

&q q

*p ≡ a

Page 8: Pointers (1A) - Wikimedia · Pointers 32 Young Won Lim 11/1/17 Variable Declarations int a ; &a a =100 The variable a holds an integer data int * p ; &p p The pointer variable p holds

Pointers 8 Young Won Lim11/1/17

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 9: Pointers (1A) - Wikimedia · Pointers 32 Young Won Lim 11/1/17 Variable Declarations int a ; &a a =100 The variable a holds an integer data int * p ; &p p The pointer variable p holds

Pointers 9 Young Won Lim11/1/17

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 10: Pointers (1A) - Wikimedia · Pointers 32 Young Won Lim 11/1/17 Variable Declarations int a ; &a a =100 The variable a holds an integer data int * p ; &p p The pointer variable p holds

Pointers 10 Young Won Lim11/1/17

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 11: Pointers (1A) - Wikimedia · Pointers 32 Young Won Lim 11/1/17 Variable Declarations int a ; &a a =100 The variable a holds an integer data int * p ; &p p The pointer variable p holds

Functions1 Overview 11 Young Won Lim

11/1/17

Variable Scopes

int main (){ int x, int y; ... ...

func1 ( 10, 20 );

... ...}

int func1 (int a, int b){ int i, int j; ... ...

... ...}

int x, int y;

int a, int b

int i, int j;

i and j’s variable scope

x and y’s variable scope

Only top stack frame is activeand its variable can be accessed

Communications are performed only through the parameter variables

( 10, 20 )

cannot access Each other

func1’s Stack Frame

main’sStackFrame

Page 12: Pointers (1A) - Wikimedia · Pointers 32 Young Won Lim 11/1/17 Variable Declarations int a ; &a a =100 The variable a holds an integer data int * p ; &p p The pointer variable p holds

Functions1 Overview 12 Young Won Lim

11/1/17

Pass by Reference

int main (){ int x, int y; ... ...

func1 ( &x, &y );

... ...}

int func1 (int* a, int* b){ int i, int j; ... ...

... ...}

func1’s Stack Frame

main’sStackFrame int x, int y;

int* a, int* b

int i, int j;

x and y’s variable scope

( &x, &y )

x and y are made known to func1func1 can read / write x and ythrough their addresses a=&x and b=&y*a and *b

xy

ab

*a*b

Page 13: Pointers (1A) - Wikimedia · Pointers 32 Young Won Lim 11/1/17 Variable Declarations int a ; &a a =100 The variable a holds an integer data int * p ; &p p The pointer variable p holds

Pointers 13 Young Won Lim11/1/17

Pass by integer reference

&a

void swap(int *p, int *q) {int tmp;

tmp = *p; *p = *q;

*q = tmp;}

a

&b b

int a, b;

swap( &a, &b );

Page 14: Pointers (1A) - Wikimedia · Pointers 32 Young Won Lim 11/1/17 Variable Declarations int a ; &a a =100 The variable a holds an integer data int * p ; &p p The pointer variable p holds

Pointers 14 Young Won Lim11/1/17

Swapping pointers

&a a

&b b

&p p

&q q

&a a

&b b

&p p

&q q

Page 15: Pointers (1A) - Wikimedia · Pointers 32 Young Won Lim 11/1/17 Variable Declarations int a ; &a a =100 The variable a holds an integer data int * p ; &p p The pointer variable p holds

Pointers 15 Young Won Lim11/1/17

Pass by integer pointer reference

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

tmp = *m; *m = *n;

*n = tmp;}

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

swap( &p, &q );

&p p

&q q

Page 16: Pointers (1A) - Wikimedia · Pointers 32 Young Won Lim 11/1/17 Variable Declarations int a ; &a a =100 The variable a holds an integer data int * p ; &p p The pointer variable p holds

Pointers 16 Young Won Lim11/1/17

Integer Pointer Types

&a

data

int a; a

address

int *p;

short *q;

&p p

&q q

char *r; &r r

Page 17: Pointers (1A) - Wikimedia · Pointers 32 Young Won Lim 11/1/17 Variable Declarations int a ; &a a =100 The variable a holds an integer data int * p ; &p p The pointer variable p holds

Pointers 17 Young Won Lim11/1/17

Pointer Type Cast

&a

data

long a;

address

int *p;

short *q;

&p p

&q q

char *r; &r r

Page 18: Pointers (1A) - Wikimedia · Pointers 32 Young Won Lim 11/1/17 Variable Declarations int a ; &a a =100 The variable a holds an integer data int * p ; &p p The pointer variable p holds

Pointers 18 Young Won Lim11/1/17

Integer Pointer Types

&a

data

int a;

address

int *p;

short *q;

&p

&q q

char *r; &r r

Page 19: Pointers (1A) - Wikimedia · Pointers 32 Young Won Lim 11/1/17 Variable Declarations int a ; &a a =100 The variable a holds an integer data int * p ; &p p The pointer variable p holds

Pointers 19 Young Won Lim11/1/17

Integer Pointer Types

&a

data

int a;

address

int *p;

short *q;

&p

&q

char *r; &r r

Page 20: Pointers (1A) - Wikimedia · Pointers 32 Young Won Lim 11/1/17 Variable Declarations int a ; &a a =100 The variable a holds an integer data int * p ; &p p The pointer variable p holds

Pointers 20 Young Won Lim11/1/17

Variables

&a

data int a;

a can hold an integer a

address

&a

a = 100;

a holds an integer 100a 100

dataaddress

Page 21: Pointers (1A) - Wikimedia · Pointers 32 Young Won Lim 11/1/17 Variable Declarations int a ; &a a =100 The variable a holds an integer data int * p ; &p p The pointer variable p holds

Pointers 21 Young Won Lim11/1/17

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 22: Pointers (1A) - Wikimedia · Pointers 32 Young Won Lim 11/1/17 Variable Declarations int a ; &a a =100 The variable a holds an integer data int * p ; &p p The pointer variable p holds

Pointers 22 Young Won Lim11/1/17

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 23: Pointers (1A) - Wikimedia · Pointers 32 Young Won Lim 11/1/17 Variable Declarations int a ; &a a =100 The variable a holds an integer data int * p ; &p p The pointer variable p holds

Pointers 23 Young Won Lim11/1/17

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 24: Pointers (1A) - Wikimedia · Pointers 32 Young Won Lim 11/1/17 Variable Declarations int a ; &a a =100 The variable a holds an integer data int * p ; &p p The pointer variable p holds

Pointers 24 Young Won Lim11/1/17

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 25: Pointers (1A) - Wikimedia · Pointers 32 Young Won Lim 11/1/17 Variable Declarations int a ; &a a =100 The variable a holds an integer data int * p ; &p p The pointer variable p holds

Pointers 25 Young Won Lim11/1/17

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 26: Pointers (1A) - Wikimedia · Pointers 32 Young Won Lim 11/1/17 Variable Declarations int a ; &a a =100 The variable a holds an integer data int * p ; &p p The pointer variable p holds

Pointers 26 Young Won Lim11/1/17

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 27: Pointers (1A) - Wikimedia · Pointers 32 Young Won Lim 11/1/17 Variable Declarations int a ; &a a =100 The variable a holds an integer data int * p ; &p p The pointer variable p holds

Pointers 27 Young Won Lim11/1/17

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 28: Pointers (1A) - Wikimedia · Pointers 32 Young Won Lim 11/1/17 Variable Declarations int a ; &a a =100 The variable a holds an integer data int * p ; &p p The pointer variable p holds

Pointers 28 Young Won Lim11/1/17

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 29: Pointers (1A) - Wikimedia · Pointers 32 Young Won Lim 11/1/17 Variable Declarations int a ; &a a =100 The variable a holds an integer data int * p ; &p p The pointer variable p holds

Pointers 29 Young Won Lim11/1/17

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 30: Pointers (1A) - Wikimedia · Pointers 32 Young Won Lim 11/1/17 Variable Declarations int a ; &a a =100 The variable a holds an integer data int * p ; &p p The pointer variable p holds

Pointers 30 Young Won Lim11/1/17

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 31: Pointers (1A) - Wikimedia · Pointers 32 Young Won Lim 11/1/17 Variable Declarations int a ; &a a =100 The variable a holds an integer data int * p ; &p p The pointer variable p holds

Pointers 31 Young Won Lim11/1/17

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 32: Pointers (1A) - Wikimedia · Pointers 32 Young Won Lim 11/1/17 Variable Declarations int a ; &a a =100 The variable a holds an integer data int * p ; &p p The pointer variable p holds

Pointers 32 Young Won Lim11/1/17

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 33: Pointers (1A) - Wikimedia · Pointers 32 Young Won Lim 11/1/17 Variable Declarations int a ; &a a =100 The variable a holds an integer data int * p ; &p p The pointer variable p holds

Pointers 33 Young Won Lim11/1/17

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 34: Pointers (1A) - Wikimedia · Pointers 32 Young Won Lim 11/1/17 Variable Declarations int a ; &a a =100 The variable a holds an integer data int * p ; &p p The pointer variable p holds

Pointers 34 Young Won Lim11/1/17

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 35: Pointers (1A) - Wikimedia · Pointers 32 Young Won Lim 11/1/17 Variable Declarations int a ; &a a =100 The variable a holds an integer data int * p ; &p p The pointer variable p holds

Pointers 35 Young Won Lim11/1/17

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 36: Pointers (1A) - Wikimedia · Pointers 32 Young Won Lim 11/1/17 Variable Declarations int a ; &a a =100 The variable a holds an integer data int * p ; &p p The pointer variable p holds

Pointers 36 Young Won Lim11/1/17

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 37: Pointers (1A) - Wikimedia · Pointers 32 Young Won Lim 11/1/17 Variable Declarations int a ; &a a =100 The variable a holds an integer data int * p ; &p p The pointer variable p holds

Pointers 37 Young Won Lim11/1/17

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

a [4]

Type of each element

Page 38: Pointers (1A) - Wikimedia · Pointers 32 Young Won Lim 11/1/17 Variable Declarations int a ; &a a =100 The variable a holds an integer data int * p ; &p p The pointer variable p holds

Pointers 38 Young Won Lim11/1/17

Array of Pointers (2)

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

a[0] a[1]

a[2] a[3]

(int) (int)

(int) (int)

b[0] b[1]

b[2] b[3]

(int *) (int *)

(int *) (int *)

int

int

int

int

a(int *) b(int * *)

b[0]

b[1]

b[2]

b[3]

Page 39: Pointers (1A) - Wikimedia · Pointers 32 Young Won Lim 11/1/17 Variable Declarations int a ; &a a =100 The variable a holds an integer data int * p ; &p p The pointer variable p holds

Pointers 39 Young Won Lim11/1/17

c[0] c[1]

c[2] c[3]

(int *) (int *)

(int *) (int *)

A 2-D Array via a double pointer

c(int * *)

int c[4] [4]

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

c[1]

c[2]

c[3]

(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]

c [i][j]

(*(c+i))[j]

*(*(c+i)+j)

Page 40: Pointers (1A) - Wikimedia · Pointers 32 Young Won Lim 11/1/17 Variable Declarations int a ; &a a =100 The variable a holds an integer data int * p ; &p p The pointer variable p holds

Pointers 40 Young Won Lim11/1/17

A 2-D array via a single pointer

c[0] c[1]

c[2] c[3]

(int *) (int *)

(int *) (int *)

c(int * *)

(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 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]

p =c[0];int *

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

Page 41: Pointers (1A) - Wikimedia · Pointers 32 Young Won Lim 11/1/17 Variable Declarations int a ; &a a =100 The variable a holds an integer data int * p ; &p p The pointer variable p holds

Pointers 41 Young Won Lim11/1/17

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: Pointers (1A) - Wikimedia · Pointers 32 Young Won Lim 11/1/17 Variable Declarations int a ; &a a =100 The variable a holds an integer data int * p ; &p p The pointer variable p holds

Pointers 42 Young Won Lim11/1/17

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: Pointers (1A) - Wikimedia · Pointers 32 Young Won Lim 11/1/17 Variable Declarations int a ; &a a =100 The variable a holds an integer data int * p ; &p p The pointer variable p holds

Pointers 43 Young Won Lim11/1/17

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 44: Pointers (1A) - Wikimedia · Pointers 32 Young Won Lim 11/1/17 Variable Declarations int a ; &a a =100 The variable a holds an integer data int * p ; &p p The pointer variable p holds

Pointers 44 Young Won Lim11/1/17

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 45: Pointers (1A) - Wikimedia · Pointers 32 Young Won Lim 11/1/17 Variable Declarations int a ; &a a =100 The variable a holds an integer data int * p ; &p p The pointer variable p holds

Pointers 45 Young Won Lim11/1/17

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 46: Pointers (1A) - Wikimedia · Pointers 32 Young Won Lim 11/1/17 Variable Declarations int a ; &a a =100 The variable a holds an integer data int * p ; &p p The pointer variable p holds

Pointers 46 Young Won Lim11/1/17

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 47: Pointers (1A) - Wikimedia · Pointers 32 Young Won Lim 11/1/17 Variable Declarations int a ; &a a =100 The variable a holds an integer data int * p ; &p p The pointer variable p holds

Pointers 47 Young Won Lim11/1/17

const type, const pointer type (1)

const int * p;

int * const q ;

const int * const r ;

Page 48: Pointers (1A) - Wikimedia · Pointers 32 Young Won Lim 11/1/17 Variable Declarations int a ; &a a =100 The variable a holds an integer data int * p ; &p p The pointer variable p holds

Pointers 48 Young Won Lim11/1/17

const type, const pointer type (2)

qqp

integerinteger

read only

integerwr

read only

wr

const int * p; int * const q ;

address address

Page 49: Pointers (1A) - Wikimedia · Pointers 32 Young Won Lim 11/1/17 Variable Declarations int a ; &a a =100 The variable a holds an integer data int * p ; &p p The pointer variable p holds

Pointers 49 Young Won Lim11/1/17

const type, const pointer type (3)

address

rr

read only

integerinteger

read only

const int * const r ;

wr

wr

Page 50: Pointers (1A) - Wikimedia · Pointers 32 Young Won Lim 11/1/17 Variable Declarations int a ; &a a =100 The variable a holds an integer data int * p ; &p p The pointer variable p holds

Pointers 50 Young Won Lim11/1/17

Pointer Types and Associated Data

data

pc

char *pc;

data

ps

short *ps;

data

pi

int *pi;

short valchar val

int val

address address address

8 bits

incr

easi

ng a

ddre

ss

Page 51: Pointers (1A) - Wikimedia · Pointers 32 Young Won Lim 11/1/17 Variable Declarations int a ; &a a =100 The variable a holds an integer data int * p ; &p p The pointer variable p holds

Pointers 51 Young Won Lim11/1/17

Pointer Types

data

pc

char *pc;

data

ps

short *ps;

data

pi

int *pi;

address address address

8 bits

incr

easi

ng a

ddre

ss

Page 52: Pointers (1A) - Wikimedia · Pointers 32 Young Won Lim 11/1/17 Variable Declarations int a ; &a a =100 The variable a holds an integer data int * p ; &p p The pointer variable p holds

Pointers 52 Young Won Lim11/1/17

Little Endian Example

data

a

b

c int a;short b;char c;

&a

&b

&c

incr

easi

ng a

ddre

ss

8 bits

data

a

b

c

&a

&b

&c

8 bits

incr

easi

ng a

ddre

ss

the order of definition

Page 53: Pointers (1A) - Wikimedia · Pointers 32 Young Won Lim 11/1/17 Variable Declarations int a ; &a a =100 The variable a holds an integer data int * p ; &p p The pointer variable p holds

Pointers 53 Young Won Lim11/1/17

int *, short *, char * type variables

int * pi;short * ps;char * pc;

pc

ps

pi

address

Not a sized representation

Page 54: Pointers (1A) - Wikimedia · Pointers 32 Young Won Lim 11/1/17 Variable Declarations int a ; &a a =100 The variable a holds an integer data int * p ; &p p The pointer variable p holds

Pointers 54 Young Won Lim11/1/17

Pointer Variable Assignment

data

char * pc; short * ps;int * pi;

int a;short b;char c;

pc

ps

pi

pi = &a;ps = &b;pc = &c;

address

8 bits

a

b

c

&a

&b

&c

Page 55: Pointers (1A) - Wikimedia · Pointers 32 Young Won Lim 11/1/17 Variable Declarations int a ; &a a =100 The variable a holds an integer data int * p ; &p p The pointer variable p holds

Pointers 55 Young Won Lim11/1/17

a

&a

a

&a

Pointer Type Casting

data

pc

char *pc;pc = (char *) &a

data

ps

data

pi

address address address

short *ps;ps = (short *) &a

int *pi;pi = (int *) &a

a

&a*ps

*pc

*pi

8 bits

Page 56: Pointers (1A) - Wikimedia · Pointers 32 Young Won Lim 11/1/17 Variable Declarations int a ; &a a =100 The variable a holds an integer data int * p ; &p p The pointer variable p holds

Pointers 56 Young Won Lim11/1/17

Accessing bytes of a variable

data

pc

char *pc;pc = (char *) &a

address

a

&a

pc+3

pc+2

pc+1

pc

*(pc+3)

data

*(pc+2)

*(pc+1)

*(pc+0)

pc

char *pc;pc = (char *) &a

address

&a

pc+3

pc+2

pc+1

pc

8 bits

Page 57: Pointers (1A) - Wikimedia · Pointers 32 Young Won Lim 11/1/17 Variable Declarations int a ; &a a =100 The variable a holds an integer data int * p ; &p p The pointer variable p holds

Pointers 57 Young Won Lim11/1/17

32-bit and 64-bit Address

32-bit machine : address : 4 bytes

64-bit machine : address : 8 bytes

pc

ps

pi

32-bit machine address :

4 bytes64-bit

machine address :

8 bytes

8 bits 8 bits

Page 58: Pointers (1A) - Wikimedia · Pointers 32 Young Won Lim 11/1/17 Variable Declarations int a ; &a a =100 The variable a holds an integer data int * p ; &p p The pointer variable p holds

Pointers 58 Young Won Lim11/1/17

64-bit machine : 8-byte address

pi ps pc

char *pc; short *ps; int *pi;

Page 59: Pointers (1A) - Wikimedia · Pointers 32 Young Won Lim 11/1/17 Variable Declarations int a ; &a a =100 The variable a holds an integer data int * p ; &p p The pointer variable p holds

Pointers 59 Young Won Lim11/1/17

64-bit machine : 8-byte address & data buses

char *pc; pc

8 bits

short *ps; ps

8 bits

int *pi; pi

8 bits

Page 60: Pointers (1A) - Wikimedia · Pointers 32 Young Won Lim 11/1/17 Variable Declarations int a ; &a a =100 The variable a holds an integer data int * p ; &p p The pointer variable p holds

Pointers 60 Young Won Lim11/1/17

32-bit machine : 4-byte address

pi ps pc

char *pc; short *ps; int *pi;

Page 61: Pointers (1A) - Wikimedia · Pointers 32 Young Won Lim 11/1/17 Variable Declarations int a ; &a a =100 The variable a holds an integer data int * p ; &p p The pointer variable p holds

Pointers 61 Young Won Lim11/1/17

64-bit machine : 8-byte address and data buses

char *pc;pc

8 bits

short *ps;ps

8 bits

int *pi;pi

8 bits

Page 62: Pointers (1A) - Wikimedia · Pointers 32 Young Won Lim 11/1/17 Variable Declarations int a ; &a a =100 The variable a holds an integer data int * p ; &p p The pointer variable p holds

Young Won Lim11/1/17

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