Pointers
Lecture 1
Pointers Introduction, Pointer Variables and Initialization, Address of
and Dereferencing Operators, Pointer operations
Usman Anwer
Lecturer
Procedural Programming (with C++)
• Try to simulate compilation and execution in your head.
• Easiest way to learn programming is to experiment.
• The computer is a laboratory
• If you don’t know something then write a program to give you the
answer
… if you can
• Otherwise, try looking in a book (or on the web)
• You can always ask someone (doesn’t have to be me)
Variations: what will happen in each case
// Example 1A: // Example 2A:
int main () int main ()
{return 0;} {}
// Example 3A: // Example 4A:
main () int main ()
{return 0;} {return 0;}
// Example 5A: // Example 6A: // Example 7A:
int main () float main () void main ()
{return 7;} {return 7.7;} {}
Pointers
• Every byte in the computer’s memory has an address.
• Addresses are numbers (just as they are for houses on a street. The
numbers start at 0 and go up from there). If you have 1MB of
memory, the highest address is 1,048,575.
• Local variables are stored on the stack and their addresses appear in
descending order, which grows downward in memory.
• Global variables are stored on the heap and their addresses appear
in ascending order, which grows upward in memory.
Pointers
A program, when it is loaded into memory,
occupies a certain range of addresses.
Addresses are displayed in hexadecimal
Format.
Sizeof() returns the no of bytes a variable is
Occupying.
Pointers
What are pointers for?
• Accessing array elements
• Passing arguments to a function when the function needs to modify
the original argument
• Passing arrays and strings to functions
• Obtaining memory from the system
• Creating data structures such as linked lists
• In C++, virtual functions, new operator, and this require the use of
pointers.
Address-of (&) and Pointers
To find the address of a variable.
int main() {
int x = 11;
Output:
int arr[] = {12, 21, 23, 34}; &x 00000009E8CFF744
int* ptr = arr; &arr 00000009E8CFF768
cout <<"&x "<< &x << endl; ptr 00000009E8CFF768
cout <<"&arr " << &arr << endl; ptr[0] 00000009E8CFF768
4 bytes
cout <<"ptr " << ptr << endl; ptr[1] 00000009E8CFF76C
cout <<"ptr[0] " << &ptr[0] << endl;
cout <<"ptr[1] " << &ptr[1] << endl;
return 0; };
Address-of (&) and Pointers
To find the address of a variable.
int x1 = 23;
int x2 = 43;
int main() { Output:
int x = 11; &x 0x7ffcc458558c
&y 0x7ffcc4585588
int y = 22; &x1 0x404030
cout <<"&x "<< &x << endl; &x2 0x404034
cout << "&y " << &y << endl;
cout << "&x1 " << &x1 << endl;
cout << "&x2 " << &x2 << endl;
return 0; };
*May vary compiler to compiler
Pointers BASICS
int var1=13
int* ptr; // pointer to integer
ptr = &var1; //pointer points to var1
cout<<ptr //000034AC
cout<<*ptr //13
• What is the difference betweeen int *, float *, char * types?
• What if the pointer is uninitialized?
int * ptr=nullptr
Pointer to array Dereferencing
const int SIZE = 5;
const int arr[SIZE] = {12,21,23,34,45};
const int* numPtr;
numPtr = arr; // numPtr=[12,21,23,34,45]
numPtr[0] // 12
*numPtr // 12 Dereferencing Operator
*(numPtr+1) // 21
numPtr++
*numPtr // 21
Points to Ponder
An array is given following with a pointer assignment:
int set[8] = {5, 10, 15, 20, 25, 30, 35, 40};
int *nums = set; // Make nums point to set
1) Explain nums < &set[7]?
2) Print the array in reverse order using pointers in while loop and ++/--
operators only?
Pointers Arithmetic
• Increment(ptr++):
If ptr is of type int* (4 bytes on most systems), ptr++ moves the
pointer forward by 4 bytes.
• Decrement(ptr--):
Moves the pointer to the previous element of its type.
• Addition(ptr+n):
Moves the pointer n elements forward.
• Subtraction(ptr-n):
Moves the pointer n elements backward.
Pointer Relational
Operator Purpose
== Checks if two pointers point to the same memory location.
!= Checks if two pointers point to different memory locations.
> Checks if one pointer points to a memory location before another.
< Checks if one pointer points to a memory location after another.
>= Checks if one pointer points to the same or an earlier memory location.
<= Checks if one pointer points to the same or a later memory location.
Points to Ponder
• Explain the difference between a pointer and a reference in C++?
• What happens if you try to dereference a null pointer? How can you
prevent such errors?
• Can you store the address of a function in a pointer?
• Explain Pointer Decay?
• Concept of Dangling Pointer?
• Can you initialize one pointer with another? What would happen if
the first pointer was invalid?
Practice Problem
• Find output of the following program:
int arr[] = {10, 20, 30};
int *ptr = arr;
cout << *(ptr++) << " " << *ptr;
Homework
• Chapter 12 of C++ Programming by D.S Malik
• Chapter 9 of C++ by Tony Gaddis