Upload
dinhcong
View
226
Download
1
Embed Size (px)
Citation preview
Young Won Lim10/23/17
Pointers (1A)
Young Won Lim10/23/17
Copyright (c) 2010 - 2013 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.
Pointers 3 Young Won Lim10/23/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
Pointers 4 Young Won Lim10/23/17
Variables and their addresses
&a
data
int a; a
address
int * p;
int **q;
&p p
&q q
Pointers 5 Young Won Lim10/23/17
Variables with initializations
&a
data
int a; a
address
int * p = &a;
int **q = &p;
&p p = &a
&q q = &p
Pointers 6 Young Won Lim10/23/17
Pointer variables and pointed addresses
p
data
int a; a
address
int * p = &a;
int **q = &p;
q p
&q q
Pointers 7 Young Won Lim10/23/17
Variables : p and *p
p
data
int a; *p
address
int * p = &a;
int **q = &p;
q p
&q q
*p ≡ a
Pointers 8 Young Won Lim10/23/17
Variables : q, *q, and **q
*q
data
int a; **q
address
int * p = &a;
int **q = &p;
q *q
&q q
**q ≡ a
*q ≡ p
Pointers 9 Young Won Lim10/23/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 );
Pointers 10 Young Won Lim10/23/17
Swapping pointers
&a a
&b b
&p p
&q q
&a a
&b b
&p p
&q q
Pointers 11 Young Won Lim10/23/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
Pointers 12 Young Won Lim10/23/17
Variables
&a
data int a;
a can hold an integer a
address
&a
a = 100;
a holds an integer 100a 100
dataaddress
Pointers 13 Young Won Lim10/23/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
Pointers 14 Young Won Lim10/23/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
*q **q
Pointers 15 Young Won Lim10/23/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
Pointers 16 Young Won Lim10/23/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
**a a
Pointers 17 Young Won Lim10/23/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
Pointers 18 Young Won Lim10/23/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
Pointers 19 Young Won Lim10/23/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
Pointers 20 Young Won Lim10/23/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
*
Pointers 21 Young Won Lim10/23/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
Pointers 22 Young Won Lim10/23/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
Pointers 23 Young Won Lim10/23/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
Pointers 24 Young Won Lim10/23/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
Pointers 25 Young Won Lim10/23/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
Pointers 26 Young Won Lim10/23/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
Pointers 27 Young Won Lim10/23/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
Pointers 28 Young Won Lim10/23/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
*
*
*
*
* *
Pointers 29 Young Won Lim10/23/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
Pointers 30 Young Won Lim10/23/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]
Pointers 31 Young Won Lim10/23/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)
Pointers 32 Young Won Lim10/23/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]
Pointers 33 Young Won Lim10/23/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 **)
Pointers 34 Young Won Lim10/23/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));
Pointers 35 Young Won Lim10/23/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
Pointers 36 Young Won Lim10/23/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 (*) [])
Pointers 37 Young Won Lim10/23/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 ];
Pointers 38 Young Won Lim10/23/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] =
}
Pointers 39 Young Won Lim10/23/17
const type, const pointer type (1)
const int * p;
int * const q ;
const int * const r ;
Pointers 40 Young Won Lim10/23/17
const type, const pointer type (2)
qqp
integerinteger
read only
integerwr
read only
wr
const int * p; int * const q ;
address address
Pointers 41 Young Won Lim10/23/17
const type, const pointer type (3)
address
rr
read only
integerinteger
read only
const int * const r ;
wr
wr
Pointers 42 Young Won Lim10/23/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
Pointers 43 Young Won Lim10/23/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
Pointers 44 Young Won Lim10/23/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
Pointers 45 Young Won Lim10/23/17
int *, short *, char * type variables
int * pi;short * ps;char * pc;
pc
ps
pi
address
Not a sized representation
Pointers 46 Young Won Lim10/23/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
Pointers 47 Young Won Lim10/23/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
Pointers 48 Young Won Lim10/23/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
Pointers 49 Young Won Lim10/23/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
Pointers 50 Young Won Lim10/23/17
64-bit machine : 8-byte address
pi ps pc
char *pc; short *ps; int *pi;
Pointers 51 Young Won Lim10/23/17
64-bit machine : 8-byte address & data buses
char *pc; pc
8 bits
short *ps; ps
8 bits
int *pi; pi
8 bits
Pointers 52 Young Won Lim10/23/17
32-bit machine : 4-byte address
pi ps pc
char *pc; short *ps; int *pi;
Pointers 53 Young Won Lim10/23/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
Young Won Lim10/23/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