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.
Byte 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 Byte Address HEX address 10 bits data 8 bits 3
Variables int a; a can hold an integer value address &a data a a = 100; address data a holds the integer 100 &a a 100 4
Pointer Variables int * p; p holds an address p can hold the address of an int data *p can hold an integer value type variable int * p; pointer to int &p p int * p; 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 value address data int a; &a a = 111 int b; &b b = b = a; 8
Assignment of an address address data int a; &a a = 111 int * p; &p p = p = &a; 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 int * p = &a; &p p p &a *p a 12
Two way to access: a and *p address &a data a a = 100 &p p *p =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
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 ); 19
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 variables types 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 Addresses long a; Increasing address a 8-byte size data type &a? which byte? 25
Little / Big Endian Ordering of Bytes 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 Increasing weight LSByte Big Endian MSByte a 0 a 1 a 2 a 3 a 4 a 5 a 6 a 7 Increasing weight 26
Increasing address, Increasing weight downward, increasing address upward, increasing address &a &a Big Endian Little Endian Increasing address Increasing weight Increasing address Increasing weight Big Endian Little Endian &a &a https://stackoverflow.com/questions/15620673/which-bit-is-the-address-of-an-integer 27
Pointer Types 28
Integer Type Variables and Their Addresses long a; Increasing address a int b; b &a short c; c &b char d; d &d &c Little Endian 29
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 30
Aligning variables of different sizes long a; int b; short c; char d; 8-byte slots 4-byte slots 2-byte slots 1-bytes slots all these slots are aligned Increasing address a Memory Alignment in the Little Endian b &a &b c &c d &d 31
Possible addresses for int values 0x3007 0x3006 0x3005 0x3004 0x3003 0x3002 0x3001 0x3000 int *p; 4 k k = 0,1, 2, 32
Possible addresses for short values 0x3007 0x3006 0x3005 0x3004 0x3003 0x3002 0x3001 0x3000 short *q; 2 k k = 0,1, 2, 33
Possible addresses for char values 0x3007 0x3006 0x3005 0x3004 0x3003 0x3002 0x3001 0x3000 char *r; 1 k k = 0,1, 2, 34
Data size at an address the same address is assumed p = q = r Increasing address int *p; p short *q; q char *r; r 35
Associated data at the pointed addresses the same address is assumed p = q = r Increasing address int *p; *p p short *q; *q q char *r; *r r 36
Incrementing / decrementing pointers the same address is assumed p = q = r Increasing address int *p; p+1 p p 1 short *q; q+1 q q 1 char *r; 37 r+1 r r 1
Dereferencing the pointers the same address is assumed p = q = r Increasing address int *p; *(p+1) *p *(p-1) p short *q; *(q+1) *q *(q-1) char *r; *(r+1) q *r *(r-1) r 38
32-bit word view the same address is assumed p = q = r 1 word = 4 bytes = 4 (8 bits) = 32 bits Increasing address Increasing address Increasing address *(p-1) *(q-1) *p *(q+1) *(p+1) *q *r int *p; short *q; char *r; 39
64-bit word view Increasing address 1 word = 8 bytes = 8 (8 bits) = 64 bits the same address is assumed p = q = r *(p-1) *(p+1) *p int *p; *(q-1) *(q+1) *q short *q; *r char *r; 40
32-bit vs. 64-bit machines address data data unit per memory access 32-bit 64-bit word size 32-bit 64-bit max address space 2^32 2^64 word 41
Pointer Type Cast 42
Changing the associated data type of an address long a; &a address of a long value int * p; address of an int value short * q; address of a short value char * r; address of a char value 43
Pointer Type Casting long a; address of a long value &a int * p; address of an int value p = (int *) &a short * q; address of a short value q = (short *) &a char * r; address of a char value r = (char *) &a 44
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 45
Re-interpretation of memory data case II char c; &c 10 20 30 40 50 60 70 80 Memory alignment constraint is not met long *a; 10 20 30 40 50 60 70 80 a = (long *) &c int *p; 10 20 30 40 50 60 70 80 p = (int *) &c short *q; 10 20 30 40 50 60 70 80 q = (short *) &c char *r; 10 20 30 40 50 60 70 80 r = &c Depending on &C, the memory alignment constraint can be broken 46
const pointers 47
const type, const pointer type (1) const int * p; int * const q ; const int * const r ; constant integer value constant integer pointer constant integer value constant integer pointer constant must not be changed must not be updated must not be written must not be assigned 48
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 49
const type, const pointer type (3) const int * const r ; read only r r address wr read only integer integer wr 50
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 51