Title: Pointers Revisited
1Pointers Revisited
Outline 1 Introduction 2
Pointer Variable Declarations and
Initialization 3 Pointer Operators
4 Calling Functions by Reference 5
Using const with Pointers 6 Pointer
Expressions and Pointer Arithmetic 7
Relationship Between Pointers and Arrays
8 Arrays of Pointers 9 Function
Pointers 10 Introduction to Character and
String Processing (a) Fundamentals of
Characters and Strings (b) String
Manipulation Functions of the String- Handling
Library
2Introduction
- Pointers
- Powerful, but difficult to master
- Simulate pass-by-reference
- Close relationship with arrays and strings
3Pointer Variable Declarations and Initialization
- Pointer variables
- Contain memory addresses as values
- Normally, variable contains specific value
(direct reference) - Pointers contain address of variable that has
specific value (indirect reference) - Indirection
- Referencing value through pointer
- Pointer declarations
- indicates variable is pointer
- int myPtr
- declares pointer to int, pointer of type int
- Multiple pointers require multiple asterisks
- int myPtr1, myPtr2
4Pointer Variable Declarations and Initialization
- Can declare pointers to any data type
- Pointer initialization
- Initialized to 0, NULL, or address
- 0 or NULL points to nothing
5Pointer Operators
- (address operator)
- Returns memory address of its operand
- Example
- int y 5int yPtryPtr y // yPtr
gets address of y - yPtr points to y
6Pointer Operators
- (indirection/dereferencing operator)
- Returns synonym for object its pointer operand
points to - yPtr returns y (because yPtr points to y).
- dereferenced pointer is lvalue
- yptr 9 // assigns 9 to y
- and are inverses of each other
7- 1 // Program Program48.cpp
- 2 // Using the and operators.
- 3 include ltiostreamgt
- 4
- 5 using stdcout
- 6 using stdendl
- 7
- 8 int main()
- 9
- 10 int a // a is an integer
- 11 int aPtr // aPtr is a pointer to an
integer - 12
- 13 a 7
- 14 aPtr a // aPtr assigned address of
a - 15
- 16 cout ltlt "The address of a is " ltlt a
- 17 ltlt "\nThe value of aPtr is " ltlt
aPtr - 18
- 19 cout ltlt "\n\nThe value of a is " ltlt a
8The address of a is 0012FED4 The value of aPtr is
0012FED4 The value of a is 7 The value of aPtr
is 7 Showing that and are inverses of each
other. aPtr 0012FED4 aPtr 0012FED4
9Calling Functions by Reference
- 3 ways to pass arguments to function
- Pass-by-value
- Pass-by-reference with reference arguments
- Pass-by-reference with pointer arguments
- return can return one value from function
- Arguments passed to function using reference
arguments - Modify original values of arguments
- More than one value returned
10Calling Functions by Reference
- Pass-by-reference with pointer arguments
- Simulate pass-by-reference
- Use pointers and indirection operator
- Pass address of argument using operator
- Arrays not passed with because array name
already pointer - operator used as alias/nickname for variable
inside of function -
11- 1 // Program Program49.cpp
- 2 // Cube a variable using pass-by-value.
- 3 include ltiostreamgt
- 4
- 5 using stdcout
- 6 using stdendl
- 7
- 8 int cubeByValue( int ) // prototype
- 9
- 10 int main()
- 11
- 12 int number 5
- 13
- 14 cout ltlt "The original value of number is
" ltlt number - 15
- 16 // pass number by value to cubeByValue
- 17 number cubeByValue( number )
- 18
- 19 cout ltlt "\nThe new value of number is "
ltlt number ltlt endl
12- 25 // calculate and return cube of integer
argument - 26 int cubeByValue( int n )
- 27
- 28 return n n n // cube local variable
n and return result - 29
- 30 // end function cubeByValue
The original value of number is 5 The new value
of number is 125
13- 1 // Program Program01.cpp
- 2 // Cube a variable using pass-by-reference
with a pointer argument. - 3
- 4 include ltiostreamgt
- 5
- 6 using stdcout
- 7 using stdendl
- 8
- 9 void cubeByReference( int ) //
prototype - 10
- 11 int main()
- 12
- 13 int number 5
- 14
- 15 cout ltlt "The original value of number is
" ltlt number - 16
- 17 // pass address of number to
cubeByReference - 18 cubeByReference( number )
- 19
14- 26 // calculate cube of nPtr modifies
variable number in main - 27 void cubeByReference( int nPtr )
- 28
- 29 nPtr nPtr nPtr nPtr // cube
nPtr - 30
- 31 // end function cubeByReference
The original value of number is 5 The new value
of number is 125
15Using const with Pointers
- const qualifier
- Value of variable should not be modified
- const used when function does not need to change
a variable - Principle of least privilege
- Award function enough access to accomplish task,
but no more - Four ways to pass pointer to function
- Nonconstant pointer to nonconstant data
- Highest amount of access
- Nonconstant pointer to constant data
- Constant pointer to nonconstant data
- Constant pointer to constant data
- Least amount of access
16Using const with Pointers
- Nonconstant pointer to Nonconstant data
- Highest amount of access
- Example
- int myPtr x
- myPtr is a regular pointer to an integer variable
x. - The value of x can be changed BOTH by direct
reference - e.g x 25
- And by indirect reference
- e.g. myPtr 25
17Using const with Pointers
- Nonconstant pointer to Constant data
- Example
- const int myPtr x
- myPtr is a regular pointer to a constant integer
variable x. - The value of x can be changed by direct
reference - e.g x 25
- But NOT by indirect reference
- e.g. myPtr 25
- In other words, the value cannot be changed via
the pointer.
18Using const with Pointers
- Constant pointer to Nonconstant data
- Example
- int const myPtr x
- myPtr is a Constant pointer to a Nonconstant
integer variable x. -
- myPtr ALWAYS points to the same memory
location. - The value of x can be changed BOTH by direct
reference - e.g x 25
- And by indirect reference
- e.g. myPtr 25
19Using const with Pointers
- Constant pointer to Constant data
- Example
- const int const myPtr x
- myPtr is a constant pointer to a constant integer
variable x. - myPtr cannot point to anything else but x.
- The value of x can be changed by direct
reference - e.g x 25
- But NOT by indirect reference
- e.g. myPtr 25
- In other words, the value cannot be changed via
the pointer.
20Using const with Pointers
- A little confusing maybe??
- The Trick
- Look to the right of the keyword const to find
out what is being declared constant. - If the data type is to the right of the keyword,
it is the value that is constant. - If the variable is to the right of the keyword
const, it is the pointer variable itself that is
constant.
21- 1 // Program Program02.cpp
- 2 // Converting lowercase letters to
uppercase letters - 3 // using a non-constant pointer to
non-constant data. - 4 include ltiostreamgt
- 5
- 6 using stdcout
- 7 using stdendl
- 8
- 9 include ltcctypegt // prototypes for
islower and toupper - 10
- 11 void convertToUppercase( char )
- 12
- 13 int main()
- 14
- 15 char phrase "characters and 32.98"
- 16
- 17 cout ltlt "The phrase before conversion
is " ltlt phrase - 18 convertToUppercase( phrase )
- 19 cout ltlt "\nThe phrase after conversion
is "
22- 26 // convert string to uppercase letters
- 27 void convertToUppercase( char sPtr )
- 28
- 29 while ( sPtr ! '\0' ) // current
character is not '\0' - 30
- 31 if ( islower( sPtr ) ) // if
character is lowercase, - 32 sPtr toupper( sPtr ) //
convert to uppercase - 33
- 34 sPtr // move sPtr to next
character in string - 35
- 36 // end while
- 37
- 38 // end function convertToUppercase
The phrase before conversion is characters and
32.98 The phrase after conversion is
CHARACTERS AND 32.98
23- 1 // Program Program03.cpp
- 2 // Printing a string one character at a
time using - 3 // a non-constant pointer to constant
data. - 4 include ltiostreamgt
- 5
- 6 using stdcout
- 7 using stdendl
- 8
- 9 void printCharacters( const char )
- 10
- 11 int main()
- 12
- 13 char phrase "print characters of a
string" - 14
- 15 cout ltlt "The string is\n"
- 16 printCharacters( phrase )
- 17 cout ltlt endl
- 18
- 19 return 0 // indicates successful
termination
24- 23 // sPtr cannot modify the character to
which it points, - 24 // i.e., sPtr is a "read-only" pointer
- 25 void printCharacters( const char sPtr )
- 26
- 27 for ( sPtr ! '\0' sPtr ) // no
initialization - 28 cout ltlt sPtr
- 29
- 30 // end function printCharacters
The string is print characters of a string
25- 1 // Program Program04.cpp
- 2 // Attempting to modify data through a
- 3 // non-constant pointer to constant data.
- 4
- 5 void f( const int ) // prototype
- 6
- 7 int main()
- 8
- 9 int y
- 10
- 11 f( y ) // f attempts illegal
modification - 12
- 13 return 0 // indicates successful
termination - 14
- 15 // end main
- 16
- 17 // xPtr cannot modify the value of the
variable - 18 // to which it points
- 19 void f( const int xPtr )
Program04.cpp(21) error C2166 l-value
specifies const object
26Using const with Pointers
- const pointers
- Always point to same memory location
- Default for array name
- Must be initialized when declared
27- 1 // Program Program05.cpp
- 2 // Attempting to modify a constant pointer
to - 3 // non-constant data.
- 4
- 5 int main()
- 6
- 7 int x, y
- 8
- 9 // ptr is a constant pointer to an
integer that can - 10 // be modified through ptr, but ptr
always points to the - 11 // same memory location.
- 12 int const ptr x
- 13
- 14 ptr 7 // allowed ptr is not const
- 15 ptr y // error ptr is const
cannot assign new address - 16
- 17 return 0 // indicates successful
termination - 18
- 19 // end main
Program05.cpp(15) error C2166 l-value
specifies const object
28- 1 // Program Program06.cpp
- 2 // Attempting to modify a constant pointer
to constant data. - 3 include ltiostreamgt
- 4
- 5 using stdcout
- 6 using stdendl
- 7
- 8 int main()
- 9
- 10 int x 5, y
- 11
- 12 // ptr is a constant pointer to a
constant integer. - 13 // ptr always points to the same
location the integer - 14 // at that location cannot be modified.
- 15 const int const ptr x
- 16
- 17 cout ltlt ptr ltlt endl
- 18
- 19 ptr 7 // error ptr is const
cannot assign new value
29- Program06.cpp(19) error C2166 l-value
specifies const object - Program06.cpp(20) error C2166 l-value
specifies const object
30- sizeof
- Unary operator returns size of operand in bytes
- For arrays, sizeof returns
- ( size of 1 element ) ( number of elements )
- If sizeof( int ) 4, then
- int myArray10
- cout ltlt sizeof(myArray)
- will print 40
- sizeof can be used with
- Variable names
- Type names
- Constant values
31- 1 // Program Program07.cpp
- 2 // Sizeof operator when used on an array
name - 3 // returns the number of bytes in the
array. - 4 include ltiostreamgt
- 5
- 6 using stdcout
- 7 using stdendl
- 8
- 9 size_t getSize( double ) // prototype
- 10
- 11 int main()
- 12
- 13 double array 20
- 14
- 15 cout ltlt "The number of bytes in the
array is " - 16 ltlt sizeof( array )
- 17
- 18 cout ltlt "\nThe number of bytes returned
by getSize is " - 19 ltlt getSize( array ) ltlt endl
32- 25 // return size of ptr
- 26 size_t getSize( double ptr )
- 27
- 28 return sizeof( ptr )
- 29
- 30 // end function getSize
The number of bytes in the array is 160 The
number of bytes returned by getSize is 4
33- 1 // Program Program08.cpp
- 2 // Demonstrating the sizeof operator.
- 3 include ltiostreamgt
- 4
- 5 using stdcout
- 6 using stdendl
- 7
- 8 int main()
- 9
- 10 char c
- 11 short s
- 12 int i
- 13 long l
- 14 float f
- 15 double d
- 16 long double ld
- 17 int array 20
- 18 int ptr array
- 19
34- 20 cout ltlt "sizeof c " ltlt sizeof c
- 21 ltlt "\tsizeof(char) " ltlt sizeof(
char ) - 22 ltlt "\nsizeof s " ltlt sizeof s
- 23 ltlt "\tsizeof(short) " ltlt sizeof(
short ) - 24 ltlt "\nsizeof i " ltlt sizeof i
- 25 ltlt "\tsizeof(int) " ltlt sizeof(
int ) - 26 ltlt "\nsizeof l " ltlt sizeof l
- 27 ltlt "\tsizeof(long) " ltlt sizeof(
long ) - 28 ltlt "\nsizeof f " ltlt sizeof f
- 29 ltlt "\tsizeof(float) " ltlt sizeof(
float ) - 30 ltlt "\nsizeof d " ltlt sizeof d
- 31 ltlt "\tsizeof(double) " ltlt sizeof(
double ) - 32 ltlt "\nsizeof ld " ltlt sizeof ld
- 33 ltlt "\tsizeof(long double) " ltlt
sizeof( long double ) - 34 ltlt "\nsizeof array " ltlt sizeof
array - 35 ltlt "\nsizeof ptr " ltlt sizeof ptr
- 36 ltlt endl
- 37
- 38 return 0 // indicates successful
termination
35- sizeof c 1 sizeof(char) 1
- sizeof s 2 sizeof(short) 2
- sizeof i 4 sizeof(int) 4
- sizeof l 4 sizeof(long) 4
- sizeof f 4 sizeof(float) 4
- sizeof d 8 sizeof(double) 8
- sizeof ld 8 sizeof(long double) 8
- sizeof array 80
- sizeof ptr 4
36Pointer Expressions and Pointer Arithmetic
- Pointer arithmetic
- Increment/decrement pointer ( or --)
- Add/subtract an integer to/from a pointer( or
, - or -) - Pointers may be subtracted from each other
- Pointer arithmetic meaningless unless performed
on pointer to array - 5 element int array on a machine using 4 byte
ints - vPtr points to first element v 0 , which is at
location 3000 - vPtr 3000
- vPtr 2 sets vPtr to 3008
- vPtr points to v 2
37Pointer Expressions and Pointer Arithmetic
- Subtracting pointers
- Returns number of elements between two addresses
- vPtr2 v 2 vPtr v 0 vPtr2 - vPtr 2
- Pointer assignment
- Pointer can be assigned to another pointer if
both of same type - If not same type, cast operator must be used
- Exception pointer to void (type void )
- Generic pointer, represents any type
- No casting needed to convert pointer to void
pointer - void pointers cannot be dereferenced
38Pointer Expressions and Pointer Arithmetic
- Pointer comparison
- Use equality and relational operators
- Comparisons meaningless unless pointers point to
members of same array - Compare addresses stored in pointers
- Example could show that one pointer points to
higher numbered element of array than other
pointer - Common use to determine whether pointer is 0
(does not point to anything)
39Relationship Between Pointers and Arrays
int b10 int bPtr bPtr b
- Arrays and pointers closely related
- Array name like constant pointer
- Pointers can do array subscripting operations
- Accessing array elements with pointers
- Element b n can be accessed by ( bPtr n )
- Called pointer/offset notation
- Addresses
- b 3 same as bPtr 3
- Array name can be treated as pointer
- b 3 same as ( b 3 )
- Pointers can be subscripted (pointer/subscript
notation) - bPtr 3 same as b 3
40- 1 // Program Program60.cpp
- 2 // Using subscripting and pointer
notations with arrays. - 3
- 4 include ltiostreamgt
- 5
- 6 using stdcout
- 7 using stdendl
- 8
- 9 int main()
- 10
- 11 int b 10, 20, 30, 40
- 12 int bPtr b // set bPtr to point to
array b - 13
- 14 // output array b using array subscript
notation - 15 cout ltlt "Array b printed with\n"
- 16 ltlt "Array subscript notation\n"
- 17
- 18 for ( int i 0 i lt 4 i )
- 19 cout ltlt "b" ltlt i ltlt " " ltlt b i
ltlt '\n'
41- 26 for ( int offset1 0 offset1 lt 4
offset1 ) - 27 cout ltlt "(b " ltlt offset1 ltlt ") "
- 28 ltlt ( b offset1 ) ltlt '\n'
- 29
- 30 // output array b using bPtr and array
subscript notation - 31 cout ltlt "\nPointer subscript
notation\n" - 32
- 33 for ( int j 0 j lt 4 j )
- 34 cout ltlt "bPtr" ltlt j ltlt " " ltlt
bPtr j ltlt '\n' - 35
- 36 cout ltlt "\nPointer/offset notation\n"
- 37
- 38 // output array b using bPtr and
pointer/offset notation - 39 for ( int offset2 0 offset2 lt 4
offset2 ) - 40 cout ltlt "(bPtr " ltlt offset2 ltlt ")
" - 41 ltlt ( bPtr offset2 ) ltlt '\n'
- 42
- 43 return 0 // indicates successful
termination - 44
42- Array b printed with
-
- Array subscript notation
- b0 10
- b1 20
- b2 30
- b3 40
-
- Pointer/offset notation where the pointer is the
array name - (b 0) 10
- (b 1) 20
- (b 2) 30
- (b 3) 40
- Pointer subscript notation
- bPtr0 10
- bPtr1 20
- bPtr2 30
- bPtr3 40
43- 1 // Program Program61.cpp
- 2 // Copying a string using array notation
- 3 // and pointer notation.
- 4 include ltiostreamgt
- 5
- 6 using stdcout
- 7 using stdendl
- 8
- 9 void copy1( char , const char ) //
prototype - 10 void copy2( char , const char ) //
prototype - 11
- 12 int main()
- 13
- 14 char string1 10
- 15 char string2 "Hello"
- 16 char string3 10
- 17 char string4 "Good Bye"
- 18
- 19 copy1( string1, string2 )
44- 26
- 27 // end main
- 28
- 29 // copy s2 to s1 using array notation
- 30 void copy1( char s1, const char s2 )
- 31
- 32 for ( int i 0 ( s1 i s2 i )
! '\0' i ) - 33 // do nothing in body
- 34
- 35 // end function copy1
- 36
- 37 // copy s2 to s1 using pointer notation
- 38 void copy2( char s1, const char s2 )
- 39
- 40 for ( ( s1 s2 ) ! '\0' s1,
s2 ) - 41 // do nothing in body
- 42
- 43 // end function copy2
string1 Hello string3 Good Bye
45Arrays of Pointers
- Arrays can contain pointers
- Commonly used to store array of strings
- char suit 4 "Hearts", "Diamonds",
"Clubs", "Spades" - Each element of suit points to char (a string)
- Array does not store strings, only pointers to
strings - suit array has fixed size, but strings can be of
any size
46Function Pointers
- Pointers to functions
- Contain address of function
- Similar to how array name is address of first
element - Function name is starting address of code that
defines function - Function pointers can be
- Passed to functions
- Returned from functions
- Stored in arrays
- Assigned to other function pointers
47Function Pointers
- Arrays of pointers to functions
- Menu-driven systems
- Pointers to each function stored in array of
pointers to functions - All functions must have same return type and same
parameter types - Menu choice ? subscript into array of function
pointers
48Fundamentals of Characters and Strings
- Character constant
- Integer value represented as character in single
quotes - 'z' is integer value of z
- 122 in ASCII
- String
- Series of characters treated as single unit
- Can include letters, digits, special characters
, -, ... - String literal (string constants)
- Enclosed in double quotes, for example
- "I like C"
- Array of characters, ends with null character
'\0' - String is constant pointer
- Pointer to strings first character
- Like arrays
49Fundamentals of Characters and Strings
- String assignment
- Character array
- char color "blue"
- Creates 5 element char array color
- last element is '\0'
- Variable of type char
- char colorPtr "blue"
- Creates pointer colorPtr to letter b in string
blue - blue somewhere in memory
- Alternative for character array
- char color b, l, u, e, \0
50Fundamentals of Characters and Strings
- Reading strings
- Assign input to character array word 20
- cin gtgt word
- Reads characters until whitespace or EOF
- String could exceed array size
- cin gtgt setw( 20 ) gtgt word
- Reads 19 characters (space reserved for '\0')
51Fundamentals of Characters and Strings
- cin.getline
- Read line of text
- cin.getline( array, size, delimiter )
- Copies input into specified array until either
- One less than size is reached
- delimiter character is input
- Example
- char sentence 80
- cin.getline( sentence, 80, '\n' )
52String Manipulation Functions of the
String-handling Library
- String handling library ltcstringgt provides
functions to - Manipulate string data
- Compare strings
- Search strings for characters and other strings
- Tokenize strings (separate strings into logical
pieces)
53String Manipulation Functions of the
String-handling Library
54String Manipulation Functions of the
String-handling Library
55- 1 // Program Program64.cpp
- 2 // Using strcpy and strncpy.
- 3 include ltiostreamgt
- 4
- 5 using stdcout
- 6 using stdendl
- 7
- 8 include ltcstringgt // prototypes for
strcpy and strncpy - 9
- 10 int main()
- 11
- 12 char x "Happy Birthday to You"
- 13 char y 25
- 14 char z 15
- 15
- 16 strcpy( y, x ) // copy contents of x
into y - 17
- 18 cout ltlt "The string in array x is " ltlt
x - 19 ltlt "\nThe string in array y is "
ltlt y ltlt '\n'
56The string in array x is Happy Birthday to
You The string in array y is Happy Birthday to
You The string in array z is Happy Birthday
57- 1 // Program Program06.cpp
- 2 // Using strtok.
- 3 include ltiostreamgt
- include ltstring.hgt
- 5 using stdcout
- 6 using stdendl
- 7
- 8 int main()
-
- char input16 "abc,d"
- char p
-
- / strtok places a NULL terminatori n front
of the token, if found / - p strtok(input, ",")
-
- if (p)
- cout ltlt \n ltlt p
-
58- 1 // Program Program67.cpp
- 2 // Using strtok.
- 3 include ltiostreamgt
- 4
- 5 using stdcout
- 6 using stdendl
- 7
- 8 include ltcstringgt // prototype for
strtok - 9
- 10 int main()
- 11
- 12 char sentence "This is a sentence
with 7 tokens" - 13 char tokenPtr
- 14
- 15 cout ltlt "The string to be tokenized
is\n" ltlt sentence - 16 ltlt "\n\nThe tokens are\n\n"
- 17
- 18 // begin tokenization of sentence
- 19 tokenPtr strtok( sentence, " " )
59- 21 // continue tokenizing sentence until
tokenPtr becomes NULL - 22 while ( tokenPtr ! NULL )
- 23 cout ltlt tokenPtr ltlt '\n'
- 24 tokenPtr strtok( NULL, " " ) //
get next token - 25
- 26 // end while
- 27
- 28 cout ltlt "\nAfter strtok, sentence " ltlt
sentence ltlt endl - 29
- 30 return 0 // indicates successful
termination - 31
- 32 // end main
60- The string to be tokenized is
- This is a sentence with 7 tokens
-
- The tokens are
-
- This
- is
- a
- sentence
- with
- 7
- tokens
-
- After strtok, sentence This
61String Manipulation Functions of the
String-handling Library
- Determining string lengths
- size_t strlen( const char s )
- Returns number of characters in string
- Terminating null character not included in length
62- 1 // Program Program67.cpp
- 2 // Using strlen.
- 3 include ltiostreamgt
- 4
- 5 using stdcout
- 6 using stdendl
- 7
- 8 include ltcstringgt // prototype for
strlen - 9
- 10 int main()
- 11
- 12 char string1 "abcdefghijklmnopqrstuvwx
yz" - 13 char string2 "four"
- 14 char string3 "Boston"
- 15
- 16 cout ltlt "The length of \"" ltlt string1
- 17 ltlt "\" is " ltlt strlen( string1 )
- 18 ltlt "\nThe length of \"" ltlt string2
- 19 ltlt "\" is " ltlt strlen( string2 )
63- The length of "abcdefghijklmnopqrstuvwxyz" is 26
- The length of "four" is 4
- The length of "Boston" is 6