(1A)
Coyright (c) 2010-2018 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 LibreOffice.
Variables and their es data int a; &a a int * ; & int **q; &q q 3
Initialization of Variables data int a = 100; &a a = 100 int * = &a; & = &a int **q = &; &q q = & 4
Traditional arrow notations data data &a a = 100 &a a = 100 & = &a & = &a &q q = & &q q = & 5
Pointed es :, q data int a; a int * = &a; q int **q = &; &q q = &a q = & 6
Dereferenced Variables : * data int a; * * a int * = &a; & int **q = &; 7
Dereferenced Variables : * int a; int * = &a; int **q = &; Address assignment = &a &a *() *(&a) * a Variable aliasing * a Relations after assignment 8
Dereferenced Variables : *q, **q data int a; *q **q **q a int * = &a; q *q *q int **q = &; &q q 9
Dereferenced Variables : *q, **q int a; int * = &a; Address assignment = &a Variable aliasing * a int **q = &; q = & *q **q a q & *(q) *(&) * q **q * **q a Relations after assignment 10
Two more ways to access a : *, **q &a a * *q **q & & q *q &q q &q q &q q a * a **q a 11
Two more ways to access a : *, **q data &a & a * **q &q q 1) Read / Write a 2) Read / Write * 3) Read / Write **q 12
Variables int a; a can hold an integer &a data a a = 100; a holds 100 &a data a 100 13
Pointer Variables int * ; can hold an int * ; holds an of a int tye data & ointer to int int * ; * holds a int tye data int * 14
Pointer to Pointer Variable int ** q; q holds an int ** q; q holds an of a ointer to int tye data ointer to ointer to int &q q int * *q; ointer to int *q holds an of a int tye data q *q int **q; **q holds a int tye data *q **q int 15
Pointer Variables Examles int a = 200; &a 0x3A0 a data 200 int * = & a; & 0x3AB 0x3A0 int ** q = & ; &q 0x3CE q = 0x3AB &q 0x3CE q 0x3AB *q 0x3A0 **q 200 16
Pointer Variable with an arrow notation data * &a 0x3A0 a data 200 & & 0x3AB 0x3A0 using an arrow notation & * 0x3AB 0x3A0 200 17
Pointer Variable q with an arrow notation data data *q **q &a 0x3A0 a 200 q *q & 0x3AB 0x3A0 &q q &q 0x3CE q = 0x3AB using an arrow notation &q q 0x3CE 0x3AB *q 0x3A0 **q 200 18
The tye view oint of ointers data (int *) (int **) Tyes 19
The different view oints of ointers **q *q (int *) *q q (int **) q &q Tyes Variables Addresses 20
Single and Double Pointer Examles (1) int a ; int * ; int **q ; a * a, *, and **q: int variables q q *q *q **q 21
Single and Double Pointer Examles (2) int a ; int * ; int * *q ; a * and *q : int ointer variables (singleointers) q q *q *q **q 22
Single and Double Pointer Examles (3) int a ; int * ; int ** q ; a * q : double int ointer variables q q *q *q **q 23
Values of double ointer variables int **, **q ; (int **) (int **) X X = q; (int *) (float *) (float) 24
Variable Declarations int a ; &a a =100 The variable a holds an integer data int * ; & 200 The ointer variable holds an, at this, an integer data is stored int * * q ; &q q *q 30 The ointer variable q holds an, at the q, another *q is stored, at the *q, an integer data **q is stored 25
Access Data Via Pointer Variables (1) int a ; &a a =100 Direct Access &a value a integer 26
Access Data Via Pointer Variables (2) int * ; & *=200 Indirect Access Dereference Oerator * the content of the ointed location & value *(&) * * integer 27
Access Data Via Pointer Variables (3) 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 28
Access Data Via Pointer Variables (4) 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 29
Access Data Via Pointer Variables (5) * int a &a a =100 *(&a) = a * * int * & *=200 *(&) = *() = * * * * int * * q &q q q *q *q **q=30 *(&q) = q *(q) = *q *(*q) = **q 30
Swaing ointers - ass by reference - double ointers 31
Swaing integer ointers & = &a & = &b &q q = &b &q q = &a a = 111 a = 111 b = 222 b = 222 32
Swaing integer ointers & = &a & = &b &q q = &b &q q = &a int *, *q; swa_ointers( &, &q ); swa_ointers( int **, int ** ); function call function rototye 33
Pass by integer ointer reference void swa_ointers (int **m, int **n) { int* tm; } tm = *m; *m = *n; *n = tm; int ** m int * *m int ** n int * *n int * tm int a, b; int *, *q; =&a, q=&b; swa_ointers( &, &q ); 34
Array of Pointers 35
Array of Pointers 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 * b [4] No. of elements = 4 Tye of each element 36
Array of Pointers variable view int a [4]; int * b [4]; a b b[0] *b[0] = 11 a[0] = 11 b[0] a[1] = 22 b[1] b[1] *b[1] = 22 a[2] = 33 b[2] a[3] = 44 b[3] b[2] *b[2] = 33 b[3] *b[3] = 44 37
Array of Pointers tye view int a [4]; int * b [4]; (int *) (int * *) (int *) (int *) (int *) (int *) 38
2-d Arrays 39
A 2-D Array int c [4] [4]; c c[0] c[1] c[2] c[3] 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] int c[4] [4] 40
A 2-D Array int c [4] [4]; (int * *) (int *) (int *) (int *) (int *) c[0] c[1] c[2] c[3] int c[4] [4] 41
A 2-D Array via a double ointer int c [4] [4]; (c [i])[j] (*(c+i))[j] *(*(c+i)+j) (c [I]) = (*(c+i)) (_)[j] = *((_)+j) int c[4] [4] 42
A 2-D Array int c [4] [4]; (int * *) (int *) (int *) (c+i) (int *) (int *) c[0] c[1] c[2] *(c+i)+j c[3] (c [i])[j] (*(c+i))[j] *(*(c+i)+j) 43
A 2-D array via a single ointer int c [4] [4]; c[0] c c[0] c[1] c[2] 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] 44
A 2-D array via a single ointer int c [4] [4]; c[0] c c[0] c[1] c[2] c[3] c[i][j] c[1] c[2] c[3] [0*4+0] [0*4+1] [0*4+2] [0*4+3] [1*4+0] [1*4+1] [1*4+2] [1*4+3] [2*4+0] [2*4+1] [2*4+2] [2*4+3] [3*4+0] [3*4+1] [3*4+2] [3*4+3] int * =c[0]; [i*4+j] 45
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] 46
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] 47
Pointer to Arrays 48
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 49
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 50
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] 51
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] = 52
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