Pointers (1A)
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 youngwlim@hotmail.com. This document was produced by using OpenOffice.
Array of Pointers (1) int a [4]; int * b [4]; Array name a holds the starting address int a [4] No. of elements = 4 Type of each element Array name b holds the starting address int * a [4] No. of elements = 4 Type of each element Pointers 3
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 4
A 2-D Array via a double pointer 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 5
A 2-D array via a single pointer int * p =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] p[i*4+j] Pointers 6
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 7
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 8
Pointer to array (1) int a [4]; int a [4] (int []) a int (*p) [4] pointer to the array of 4 elements a[0] a[1] a[2] a[3] int m ; an integer variable int *n ; a pointer variable int func (int a, int b) ; a prototype int (* fp) (int a, int b) ; a function's type int * fp (int a, int b) ; function pointer Pointers 9
Pointer to array (2) int (*p) [4] ; (int (*) []) p int a [4] p (int []) *p a *p = a (*p) = a &(*p) = &a p = &a sizeof(p)= 4 bytes sizeof(*p)= 16 bytes a[0] a[1] a[2] a[3] an array with 4 integer elements Pointers 10
Pointer to array (3) &p int (*p) [4] ; int c[4] [4] (int (*) []) p p = c (*p) [ i ][ j ]; a 2-d array with 4 rows and 4 columns (int (*) []) p (int []) (int []) (int []) (int []) c *p c[0] c[1] c[2] c[3] *(p+0) *(p+1) *(p+2) *(p+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 11
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 12
const type, const pointer type (1) const int * p; int * const q ; const int * const r ; Pointers 13
const type, const pointer type (2) const int * p; int * const q ; read only integer integer wr integer p address read only q q address wr Pointers 14
const type, const pointer type (3) const int * const r ; read only r r address wr read only integer integer wr Pointers 15
Pointer Types and Associated Data 8 bits data data data increasing address char val short val int val pc ps pi address address address char *pc; short *ps; int *pi; Pointers 16
Pointer Types 8 bits data data data increasing address pc ps pi address address address char *pc; short *ps; int *pi; Pointers 17
Little Endian Example 8 bits 8 bits data data &a increasing address &c &b c b a int a; short b; char c; the order of definition &b &c a b c increasing address &a Pointers 18
int *, short *, char * type variables int * pi; short * ps; char * pc; pi ps pc address Not a sized representation Pointers 19
Pointer Variable Assignment 8 bits &c &b &a data c b a pi ps pc address char * pc; short * ps; int * pi; int a; short b; char c; pi = &a; ps = &b; pc = &c; Pointers 20
Pointer Type Casting 8 bits data data data &a a *pc &a a *ps &a *pi a pc ps pi address address address char *pc; pc = (char *) &a short *ps; ps = (short *) &a int *pi; pi = (int *) &a Pointers 21
Accessing bytes of a variable 8 bits data data pc+3 *(pc+3) pc+3 a pc+2 pc+1 *(pc+2) *(pc+1) pc+2 pc+1 &a pc &a *(pc+0) pc pc pc address address char *pc; pc = (char *) &a char *pc; pc = (char *) &a Pointers 22
32-bit and 64-bit Address 32-bit machine : address : 4 bytes 64-bit machine : address : 8 bytes 8 bits 8 bits pi ps pc 64-bit machine address : 8 bytes 32-bit machine address : 4 bytes Pointers 23
64-bit machine : 8-byte address pi ps pc char *pc; short *ps; int *pi; Pointers 24
64-bit machine : 8-byte address & data buses 8 bits char *pc; pc 8 bits short *ps; ps 8 bits int *pi; pi Pointers 25
32-bit machine : 4-byte address pi ps pc char *pc; short *ps; int *pi; Pointers 26
64-bit machine : 8-byte address and data buses 8 bits pc char *pc; 8 bits ps short *ps; 8 bits pi int *pi; Pointers 27
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