0% found this document useful (0 votes)
7 views12 pages

ITC Lect 18 (Pointers and Strings - II)

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views12 pages

ITC Lect 18 (Pointers and Strings - II)

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 12

Lecture 18: Pointers and Strings CS 101: Introduction to Computing

Pointers and Strings - II

Dr. Zahid Halim

Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi


Lecture 18: Pointers and Strings CS 101: Introduction to Computing

Example
int main() {
int x = 10;
int* y = &x;
cout << *y;
}
Prints the value 10 – the value in the variable at the address stored in y
In other words, the value to which y points

Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi


Lecture 18: Pointers and Strings CS 101: Introduction to Computing

Another Example
int main() {
int x = 10;
int* y = &x;
x = 20;
cout << *y;
}
Prints the value 20

Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi


Lecture 18: Pointers and Strings CS 101: Introduction to Computing

A Third Example
int main() {
int x = 10;
int* y = &x;
*y = 20;
cout<<x<<*y;
}
Prints: 20, 20 – we can use *y as a left-hand value, which changes the contents
of the address that y points to

Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi


Lecture 18: Pointers and Strings CS 101: Introduction to Computing

A Last Example

int main() {
int x = 10;
int* y = &x;
y = 20;
cout<<x<<*y;
}
Prints: 10, ??? – the second term will be what ever happens to be at
bytes 20, 21, 22 and 23 – could be junk.
It will look at all four bytes because it’s a pointer to an int – which is a
four-byte data structure

Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi


Lecture 18: Pointers and Strings CS 101: Introduction to Computing

Pointer 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 is meaningless unless performed on an 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
location
– vPtr += 2; sets vPtr to 3008 3000 3004 3008 3012 3016

• vPtr points to v[ 2 ] v[0] v[1] v[2] v[3] v[4]

pointer variable vPtr

Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi


Lecture 18: Pointers and Strings CS 101: Introduction to Computing

Pointer Expressions and Pointer Arithmetic


• Subtracting pointers
– Returns the number of elements between two addresses
vPtr2 = v[ 2 ];
vPtr = v[ 0 ];
vPtr2 - vPtr == 2
• Pointer comparison
– Test which pointer points to the higher numbered array element
– Test if a pointer points to 0 (NULL)
if ( vPtr == ‘0’ )
statement

Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi


Lecture 18: Pointers and Strings CS 101: Introduction to Computing

Pointer Expressions and Pointer Arithmetic


• Pointers assignment
– If not the same type, a cast operator must be used
– Exception: pointer to void (type void *)
• Generic pointer, represents any type
• No casting needed to convert a pointer to void pointer
• void pointers cannot be dereferenced

Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi


Lecture 18: Pointers and Strings CS 101: Introduction to Computing

The Relationship Between Pointers and Arrays


• Arrays and pointers closely related
– Array name like constant pointer
– Pointers can do array subscripting operations
– Having declared an array b[ 5 ] and a pointer bPtr
• bPtr is equal to b
bptr == b
• bptr is equal to the address of the first element of b
bptr == &b[ 0 ]

Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi


Lecture 18: Pointers and Strings CS 101: Introduction to Computing

The Relationship Between Pointers and Arrays


• Accessing array elements with pointers
– Element b[ n ] can be accessed by *( bPtr + n )
• Called pointer/offset notation
– Array itself can use pointer arithmetic.
• b[ 3 ] same as *(b + 3)
– Pointers can be subscripted (pointer/subscript notation)
• bPtr[ 3 ] same as b[ 3 ]

Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi


Lecture 18: Pointers and Strings CS 101: Introduction to Computing

Arrays of Pointers
• Arrays can contain pointers
– Commonly used to store an array of strings
char *suit[ 4 ] = {"Hearts", "Diamonds",
"Clubs", "Spades" };
– Each element of suit is a pointer to a char * (a string)
– The strings are not in the array, only pointers to the strings are in the
array
suit[0] ’H’ ’e’ ’a’ ’r’ ’t’ ’s’ ’\
suit[1] ’D’ ’i’ ’a’ ’m’ ’o’ ’n’ 0’
’d’ ’s’ ’\
suit[2] ’C’ ’l’ ’u’ ’b’ ’s’ ’\ 0’

suit[3] ’S’ ’p’ ’a’ ’d’ ’e’ 0’


’s’ ’\
0’
– suit array has a fixed size, but strings can be of any size

Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi


Lecture 18: Pointers and Strings CS 101: Introduction to Computing

References
Dietal and Dietal : How to Program C++
3rd Edition

Ghulam Ishaq Khan Institute of Engineering Sciences and Technology, Topi

You might also like