Pointers (1A)
Coyright (c) 2010-2013 Young W. Lim. Permission is granted to coy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.2 or any later version ublished by the Free Software Foundation; with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts. A coy of the license is included in the section entitled "GNU Free Documentation License". Please send corrections (or suggestions) to youngwlim@hotmail.com. This document was roduced by using OenOffice.
Address and Data in a Memory 10 bits 1K x 8 Memory 8 bits 0x3FF 0x3FE 0x3FD 0x3FC 2 10 = 1024 0x003 0x002 0x001 0x000 HEX 10 bits 8 bits Pointers 3
Variables int a; a can hold an integer &a a a = 100; a holds an integer 100 &a a 100 Pointers 4
Pointer Variables int * ; holds an int * ; holds an of a int tye & ointer to int int * ; * holds an integer int * Pointers 5
Pointer to Pointer Variable int ** q; q holds an int ** q; ointer to ointer to int q holds an of a ointer to int tye &q q int * *q; ointer to int *q holds an of a int tye variable q *q int **q; **q holds an integer *q **q int Pointers 6
Pointer Variables int a; int * = & a; &q 0x3CE q = 0x3AB int ** q = & ; & 0x3AB 0x3A0 &a 0x3A0 a 200 &q 0x3CE q 0x3AB *q 0x3A0 **q 200 Pointers 7
Pointer Variables &q q &q 0x3CE q = 0x3AB q *q & 0x3AB 0x3A0 **a a &a 0x3A0 a 200 &q 0x3CE q 0x3AB *q 0x3A0 **q 200 Pointers 8
Pointer Variables int * ; &q 0x3CE q = 0x3AB & 0x3CE = 0x3AB q 0x3AB *q 0x3A0 0x3AB * 200 *q 0x3A0 **q 200 & * 0x3CE 0x3AB 200 &q 0x3CE q 0x3AB *q 0x3A0 **q 200 Pointers 9
Pointer to Pointer Variable int ** q; &q 0x3CE q = 0x3AB q holds an q 0x3AB *q 0x3A0 int ** q; ointer to ointer to int q holds an of a ointer to int tye *q 0x3A0 **q 200 int * *q; ointer to int *q holds an of a int tye variable int **q; **q holds an integer &q 0x3CE q 0x3AB *q 0x3A0 **q 200 int Pointers 10
Interretation of Pointer (1) (int **) q (int *) q (int *) *q (int **) q *q (int *) * *q **q Pointers 11
Interretation of Pointer (2) The of a variable : Address of oerator & content of az ointer : Dereferencing oerator * & & * * * * Pointers 12
Integer Pointer Examles (1) int i; int tye i int * int ** i; qi; int * tye (int *) i i *i i holds an integers i holds an of a int tye int ** tye (int **) qi qi holds an of a ointer to int tye int * tye qi (int *) *qi *qi **qi Pointers 13
Integer Pointer Examles (2) int i; int * i; int ** qi; i holds an integers (int **) qi X X (int *) i holds an of Int tye (float *) qi holds an of Pointer to Int tye (float) Pointers 14
Integer Pointer Examles (3) int i = 200; tyes int ** qi &qi qi = &i int * i = &i; int ** qi = &i; int * i &i i = &i i holds an integers int i &i i =200 i holds an of a int tye qi holds an of a ointer to Int tye *qi = i *i = i *qi = i = &i **qi = *i = i Pointers 15
Variable Declarations int a ; &a a =100 The variable a holds an integer int * ; & 200 The ointer variable holds an, where an integer is stored int * * q ; &q q 30 The ointer variable q holds an, where another is stored, where an integer is stored Pointers 16
Access Data Via Pointer Variables (1) int a ; &a a =100 Direct Access &a value a integer int * ; & *=200 Indirect Access Dereference Oerator * the content of the ointed location & value *(&) * * integer Pointers 17
Access Data Via Pointer Variables (2) int * * q ; &q q q *q *q **q=30 Double Indirect Access &q value q Dereference Oerator * the content of the ointed location *(&q) *q q *q Dereference Oerator * the content of the ointed location *q **q *q **q integer Pointers 18
Access Data Via Pointer Variables (3) int a ; &a a =100 Direct Access &a value a integer int * ; & *=200 Indirect Access Dereference Oerator * the content of the ointed location value & * integer int * * q ; &q q q *q *q **q=30 Double Indirect Access Dereference Oerator * the content of the ointed location value &q q q *q *q **q integer Pointers 19
Access Data Via Pointer Variables (4) * int a &a a =100 *(&a) = a * * int * & *=200 *(&) = *() = * * * * int * * q &q q q *q *q **q=30 *(&q) = q *(q) = *q *(*q) = **q Pointers 20
Array of Pointers (1) int a [4]; int * b [4]; Array name a holds the starting int a [4] No. of elements = 4 Tye of each element Array name b holds the starting int * a [4] No. of elements = 4 Tye of each element Pointers 21
Array of Pointers (2) int a [4]; int * b [4]; (int *) a (int * *) b b[0] int a[0] (int *) b[0] a[1] (int *) b[1] b[1] int a[2] (int *) b[2] a[3] (int *) b[3] b[2] int b[3] int Pointers 22
A 2-D Array via a double ointer int c [4] [4]; (int * *) c (int *) c[0] (int *) c[1] (int *) c[2] (int *) c[3] 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] c [i][j] (*(c+i))[j] *(*(c+i)+j) Pointers 23
A 2-D array via a single ointer int * =c[0]; int c [4] [4]; c[0] (int * *) c (int *) c[0] (int *) c[1] (int *) c[2] (int *) c[3] c[i][j] 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] 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] [i*4+j] Pointers 24
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 (int *) (int *) (int *) (int *) d[0] d[1] d[2] d[3] Pointers 25
2-D Array Dynamic Memory Allocation (2) int ** d ; d = (int **) malloc (4 * size of (int *)); for (i=0; i<4; ++i) d[i] = (int *) malloc(4 * sizeof); &d (int **) d (int *) (int *) (int *) (int *) d[0] d[1] d[2] d[3] d[0][0] d[0][1] d[0][2] d[0][3] d[1][0] d[1][1] d[1][2] d[1][3] d[2][0] d[2][1] d[2][2] d[2][3] d[3][0] d[3][1] d[3][2] d[3][3] Pointers 26
Pointer to array (1) int a [4]; int a [4] (int []) a int (*) [4] ointer to the array of 4 elements a[0] a[1] a[2] a[3] int m ; an integer variable int *n ; a ointer variable int func (int a, int b) ; a rototye int (* f) (int a, int b) ; a function's tye int * f (int a, int b) ; function ointer Pointers 27
Pointer to array (2) int (*) [4] ; (int (*) []) int a [4] (int []) * a * = a (*) = a &(*) = &a = &a sizeof()= 4 bytes sizeof(*)= 16 bytes a[0] a[1] a[2] a[3] an array with 4 integer elements Pointers 28
Pointer to array (3) & int (*) [4] ; int c[4] [4] (int (*) []) = c (*) [ i ][ j ]; a 2-d array with 4 rows and 4 columns (int (*) []) (int []) (int []) (int []) (int []) c * c[0] c[1] c[2] c[3] *(+0) *(+1) *(+2) *(+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] Pointers 29
Pointer to array (4) int c [4][4]; int (*) [4]; = c; func(,... ); void func(int (*x)[4],... ) { } x[r][c] = void func(int x[ ][4],... ) { } x[r][c] = Pointers 30
const tye, const ointer tye (1) const int * ; int * const q ; const int * const r ; Pointers 31
const tye, const ointer tye (2) const int * ; int * const q ; read only integer integer wr integer read only q q wr Pointers 32
const tye, const ointer tye (3) const int * const r ; read only r r wr read only integer integer wr Pointers 33
Pointer Tyes and Associated Data 8 bits increasing char val short val int val c s i char *c; short *s; int *i; Pointers 34
Pointer Tyes 8 bits increasing c s i char *c; short *s; int *i; Pointers 35
Little Endian Examle 8 bits 8 bits &a increasing &c &b c b a int a; short b; char c; the order of definition &b &c a b c increasing &a Pointers 36
int *, short *, char * tye variables int * i; short * s; char * c; i s c Not a sized reresentation Pointers 37
Pointer Variable Assignment 8 bits &c &b &a c b a i s c char * c; short * s; int * i; int a; short b; char c; i = &a; s = &b; c = &c; Pointers 38
Pointer Tye Casting 8 bits &a a *c &a a *s &a *i a c s i char *c; c = (char *) &a short *s; s = (short *) &a int *i; i = (int *) &a Pointers 39
Accessing bytes of a variable 8 bits c+3 *(c+3) c+3 a c+2 c+1 *(c+2) *(c+1) c+2 c+1 &a c &a *(c+0) c c c char *c; c = (char *) &a char *c; c = (char *) &a Pointers 40
32-bit and 64-bit Address 32-bit machine : : 4 bytes 64-bit machine : : 8 bytes 8 bits 8 bits i s c 64-bit machine : 8 bytes 32-bit machine : 4 bytes Pointers 41
64-bit machine : 8-byte i s c char *c; short *s; int *i; Pointers 42
64-bit machine : 8-byte & buses 8 bits char *c; c 8 bits short *s; s 8 bits int *i; i Pointers 43
32-bit machine : 4-byte i s c char *c; short *s; int *i; Pointers 44
64-bit machine : 8-byte and buses 8 bits c char *c; 8 bits s short *s; 8 bits i int *i; Pointers 45
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 Exress, I. K. Chun