Chapter 5 - Pointers and Strings: 2003 Prentice Hall, Inc. All Rights Reserved
Chapter 5 - Pointers and Strings: 2003 Prentice Hall, Inc. All Rights Reserved
5.1 Introduction
• Pointers
– Powerful, but difficult to master
– Simulate pass-by-reference
– Close relationship with arrays and strings
7
– Normally, variable contains specific value (direct reference)
– Pointers contain address of variable that has specific value
(indirect reference) countPtr count
7
• 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;
y yptr y
5 500000 600000 600000 5
yPtr
address of y
is value of
yptr
• * (indirection/dereferencing operator)
– Returns synonym for object its pointer operand points to
– i.e.:cout<<*yPtr<<endl;
– and cout<<y<<endl; returns the same value (value of y)
– *yPtr returns y (because yPtr points to y).
– a dereferenced pointer can also be used as an lvalue
*yptr = 9; // assigns 9 to y
• * and & are inverses of each other
fig05_04.cpp
The address of a is 0012FED4
(2 of 2)
The value of aPtr is 0012FED4
The value of a is 7
fig05_04.cpp
The value of *aPtr is 7 output (1 of 1)
Showing that * and & are inverses of each other.
&*aPtr = 0012FED4
*&aPtr = 0012FED4 * and & are inverses; same
result when both applied to
aPtr
location
3000 3004 3008 3012 3016
– int v[5]
– vPtr = v or vPtr = &v[0]
– 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 ]
• 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
• 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)
• 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 string’s first character
– Like arrays
2003 Prentice Hall, Inc. All rights reserved.
27
• 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’ };
• cin.getline
– Read line of text
– takes 3 arguments
• a character array in which the line is to be stored
• a length
• a delimiter character
– 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' );
int strcmp( const char *s1, Compares the string s1 with the string s2. The
const char *s2 ); function returns a value of zero, less than zero
or greater than zero if s1 is equal to, less than
or greater than s2, respectively.
s1 = Happy
s2 = New Year
After strcat(s1, s2):
s1 = Happy New Year
s2 = New Year
After strncat(s3, s1, 6):
s1 = Happy New Year
s3 = Happy
After strcat(s3, s1):
s1 = Happy New Year
s3 = Happy Happy New Year
strcmp(s1, s2) = 0
strcmp(s1, s3) = 1
strcmp(s3, s1) = -1
strncmp(s1, s3, 6) = 0
strncmp(s1, s3, 7) = 1
strncmp(s3, s1, 7) = -1
fig05_32.cpp
output (1 of 1)