Pointers (1A)
Copyright (c) 2010-2018 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 LibreOffice.
Address and Data in a Memory address data address 10 bits 1K x 8 Memory data 8 bits 0x3FF 0x3FE 0x3FD 0x3FC 2 10 = 1024 0x003 0x002 0x001 0x000 HEX address 10 bits data 8 bits 3
Variables int a; a can hold an integer address &a data a a = 100; a holds an integer 100 address &a data a 100 4
Pointer Variables int * p; p holds an address int * p; p holds an address of a int type data &p p pointer to int int * p; *p holds an integer int p *p 5
Dereferencing The address of a variable : Address of operator & The content of a pointed location : Dereferencing operator * &p p p & p p * *p p * *p 6
Variables and their addresses address data int a; &a a int * p; &p p 7
Assignment of a variable address data int a; &a a = 111 int b; &b b = 8
Assignment of an address address data int a; &a a = 111 int * p; &p p = 9
Variables with initializations address data int a; &a a int * p = &a; &p p = &a 10
Pointed addresses : p address data int a; p a int * p = &a; &p p p &a 11
Dereferenced Variable : *p address data int a; p *p *p a int * p = &a; &p p p &a *p a 12
Another way to access a : *p address data &a &p a p *p =100 a = 100 1) Read/Write a 2) Read/Write *p 13
1. Pass by Reference 2. Arrays 14
Pass by Reference 15
Variable Scopes int func1 (int a, int b) { int i, int j;...... }...... i and j s variable scope cannot access each other Only top stack frame is active and its variable can be accessed Communications are performed only through the parameter variables int main () { int x, int y;...... x and y s variable scope func1 s Stack Frame int i, int j; int a, int b a b } func1 ( 10, 20 );...... main s Stack Frame ( 10, 20 ) int x, int y; x y 16
Pass by Reference int func1 (int* a, int* b) { int i, int j;...... x and y are made known to func1 func1 can read / write x and y through their addresses }...... *a *b a=&x b=&y int main () { int x, int y;...... } func1 ( &x, &y );...... func1 s Stack Frame int i, int j; int* a, int* b x and y s variable scope ( &x, &y ) main s Stack Frame int x, int y; a b x y *a *b 17
Swapping integers &a a = 111 &a a = 222 &b b = 222 &b b = 111 int a, b; swap( &a, &b ); swap( int *, int *); function call function prototype 18
Integer and Integer Pointer Types int *m int *n integer pointer declarations a way of thinking int * int * m n m n integer pointer variables int * int *m int *n *m *n treated as integer variables int 19
Pass by integer reference void swap(int *p, int *q) { int tmp; } tmp = *p; *p = *q; *q = tmp; int * p int *q int * p int *q int tmp int a, b; swap( &a, &b ); 20
Arrays 21
Accessing array elements using an address int x[5]; x holds the starting address of 5 consecutive int variables 5 int variables address data index data cannot change address x (constant) x x + 1 *x *(x+1) 0 1 2 3 4 x[0] x[1] x[2] x[3] x[4] x + 2 x + 3 x + 4 *(x+2) *(x+3) *(x+4) 22
Accessing an Array with a Pointer Variable int x [5] = { 1, 2, 3, 4, 5 }; int *p = x; &x x &p p x is a constant symbol cannot be changed p[0] p[1] p[2] p[3] p[4] x[0] x[1] x[2] x[3] x[4] 80 90 40 70 60 *(x+0) *(x+1) *(x+2) *(x+3) *(x+4) *(p+0) *(p+1) *(p+2) *(p+3) *(p+4) p is a variable can point to other addresses 23
Byte Address Little Endian Big Endian 24
Byte Address long a; Increasing address a &a? 25
Numbers in Positional Notation long a = 0x1020304050607080; 8 (bytes) a 7 a 6 a 5 a 4 a 3 a 2 a 1 a 0 Most Significant Byte Least Significant Byte a 7 = 0 x 10 a 6 = 0 x 20 a 5 = 0 x 30 a 4 = 0 x 40 a 3 = 0 x 50 a 2 = 0 x 60 a 1 = 0 x70 a 0 = 0 x 80 16 7 16 6 16 5 16 4 16 3 16 2 16 1 16 0 the highest weight the lowest weight 26
Little / Big Endian long a; Increasing address a MSByte Little Endian LSByte a 7 a 6 a 5 a 4 a 3 a 2 a 1 a 0 LSByte Big Endian MSByte a 0 a 1 a 2 a 3 a 4 a 5 a 6 a 7 27
Little Endian Byte Address Example Increasing address 0x3007 0x3006 0x3005 0x3004 0x3003 0x3002 0x3001 0x3000 long a; a 7 a 0 Increasing weight &a MSByte Little Endian LSByte 28
Big Endian Byte Address Example Increasing address 0x3007 0x3006 0x3005 0x3004 0x3003 0x3002 0x3001 0x3000 long a; a 0 a 7 Increasing weight &a LSByte Big Endian MSByte 29
Representations of Endianness downward, increasing address upward, increasing address &a &a Big Endian Little Endian 0x3000 a 7 0x3000 a 0 0x3007 a 0 0x3007 a 7 0x3001 0x3001 0x3006 0x3006 0x3002 0x3002 0x3005 0x3005 0x3003 0x3003 0x3004 0x3004 0x3004 0x3004 0x3003 0x3003 0x3005 0x3005 0x3002 0x3002 0x3006 0x3006 0x3001 0x3001 0x3007 a 0 0x3007 a 7 0x3000 a 7 0x3000 a 0 Big Endian Little Endian &a &a https://stackoverflow.com/questions/15620673/which-bit-is-the-address-of-an-integer 30
Little / Big Endian Processors Processor Endianness Motorola 68000 Big Endian PowerPC (PPC) Big Endian Sun Sparc Big Endian IBM S/390 Big Endian Intel x86 (32 bit) Little Endian Intel x86_64 (64 bit) Little Endian Dec VAX Little Endian Alpha (Big/Little) Endian ARM (Big/Little) Endian IA-64 (64 bit) (Big/Little) Endian MIPS (Big/Little) Endian http://www.yolinux.com/tutorials/endian-byte-order.html 31
Pointer Types 32
Integer Type Variables and Byte Addresses long a; Increasing address a int b; b &a short c; c &b char d; d &d &c Little Endian 33
Points to the LSByte Increasing address a &p p long *p; b &q q int *q; c &r r short *r; d &s s char *s; Little Endian 34
Sizes of Integer Types Increasing address long a; sizeof(long) 8 (bytes) a &a int b; sizeof(int) 4 (bytes) b &b short c; sizeof(short) 2 (bytes) c &c char d; sizeof(char) 1 (bytes) d Memory Alignment in the Little Endian &d 35
Pointer to integer values 0x3007 0x3006 0x3005 0x3004 0x3003 0x3002 0x3001 0x3000 int *p; 36
Pointer to short values 0x3007 0x3006 0x3005 0x3004 0x3003 0x3002 0x3001 0x3000 short *q; 37
Pointer to char values 0x3007 0x3006 0x3005 0x3004 0x3003 0x3002 0x3001 0x3000 char *r; 38
Initial pointer values Increasing address int *p; p short *q; q char *r; r 39
Incrementing / decrementing pointer values Increasing address int *p; p+1 p p-1 short *q; q+1 q q-1 char *r; 40 r+1 r r-1
Memory Alignment (1) - allocation of variables Memory Alignment: the data address is a multiple of the data size. enforced by compilers 0x3007 0x3006 0x3005 0x3004 0x3003 0x3002 0x3001 0x3000 efficient memory access int a; short b; char c; 41
Memory Alignment (2) integer multiple 0x3007 0x3006 0x3005 0x3004 0x3003 0x3002 0x3001 0x3000 int *p; k = 0,1, 2, 4 k short *q; char *r; 42 2 k 1 k
Memory Alignment (3) 0x3007 0x3006 0x3005 0x3004 0x3003 0x3002 0x3001 0x3000 int *p; short *q; char *r; 43
Memory Alignment (4) 0x3007 0x3006 0x3005 0x3004 0x3003 0x3002 0x3001 0x3000 10 20 30 40 50 60 70 80 int *p; p+1 p short *q; q+3 q+2 q+1 q char *r; r+7 r+6 r+5 r+4 r+3 r+2 r+1 r 44
Pointer Type Cast 45
Re-interpretation of memory data case I &a long a; 0x1020304050607080 int *p; 0x50607080 p = (int *) &a short *q; 0x7080 q = (short *) &a char *r; 80 r = (char *) &a 46
Pointer Type Cast &a long a; 0x10203040 0x50607080 starting address int *p; p p = (int *) &a short *q; char *r; 47
Integer Pointer Types &a long a; 0x1020 0x3040 0x5060 0x7080 int *p; short *q; q q = (short *) &a char *r; 48
Integer Pointer Types &a long a; 10 20 30 40 50 60 70 80 int *p; short *q; char *r; r r = (char *) &a 49
Re-interpretation of memory data case II char a; &a 10 20 30 40 50 60 70 80 Memory alignment constraint is not met long *o; o = (long *) &a int *p; p = (int *) &a short *q; q = (short *) &a char *r; r = &a In this case, the memory alignment constraint can be broken 50
const pointers 51
const type, const pointer type (1) const int * p; int * const q ; const int * const r ; read only integer value read only integer pointer read only integer value read only integer pointer 52
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 53
const type, const pointer type (3) const int * const r ; read only r r address wr read only integer integer wr 54
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; 55
Pointer Types 8 bits data data data increasing address pc ps pi address address address char *pc; short *ps; int *pi; 56
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 57
int *, short *, char * type variables int * pi; short * ps; char * pc; pi ps pc address Not a sized representation 58
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; 59
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 60
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 61
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 62
64-bit machine : 8-byte address pi ps pc char *pc; short *ps; int *pi; 63
64-bit machine : 8-byte address & data buses 8 bits char *pc; pc 8 bits short *ps; ps 8 bits int *pi; pi 64
32-bit machine : 4-byte address pi ps pc char *pc; short *ps; int *pi; 65
64-bit machine : 8-byte address and data buses 8 bits pc char *pc; 8 bits ps short *ps; 8 bits pi int *pi; 66
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 67