Lecture 3a - Pointers in C++ Spring 2025
Lecture 3a - Pointers in C++ Spring 2025
Lecture 3
C++ Pointers
Spring 2025
Lecture Contents
▪ Introduction to Pointers
▪ Pointer initialization
▪ Referencing
▪ Dereferencing
▪ Arithmetic on Pointers
Regular Variables vs Pointers
▪ Variables are used to keep track of data in the computer’s memory.
▪ Declaring a regular variable = Allocating a location in
memory to store a value
▪ Example:
int myInt = 0; //Allocates enough space in memory
//to store an int and puts 0 there
4
Regular Variables vs. Pointer Variables
▪ A pointer variable is used to hold an address
• Use operator * to declare a pointer variable
• Example:
int *pMyInt; //Declares a pointer
5
Pointer Variable
▪ The main difference between regular variables and pointers is that, variables
contain a value, while pointers contain a memory address.
▪ Memory address:
▪ We know each memory address contain some value.
▪ Thus if pointers contain memory addresses, we can get its value (indirectly).
myInt
… 7 3 4 …
172 173 174 175 176 177 178 179 180 181
1. myInt is a Memory
variable
PmyInt
PmyInt is a Pointer that … 174 3 4 …
holds the address of myInt 832 833 834 835 836 837 838 839 840 841
Dereferencing a Pointer
▪ Accessing the object addressed by the pointer
▪ Dereference Operator ’*’ used with the name of the pointer, after it is declared
and initialized
▪ Examples
int myInt = 0; //Allocates memory, stores 0
int *pMyInt; //Declares an empty pointer
pMyInt = &myInt; //Puts address in the pointer
cout << *pMyInt << endl; //Prints 0
*pMyInt = 5; //puts 5 into myInt
cout << myInt << endl; //Prints 5
cout << *pMyInt << endl; //Also prints 5
cout << pMyInt << endl; //What prints? address of the pMyInt
7
Where are the errors?
int main(){
int m;
int *pm;
*pm = 5;
int n;
int *pn = &n;
pn = 5;
}
8
Where are the errors?
int main(){
ERROR! No address in pm
int m;
//Correction
int *pm; pm = &m;
*pm = 5; *pm = 5;
int n;
int *pn = &n;
pn = 5;
}
9
Where are the errors?
int main(){
ERROR! No address in pm
int m;
//Correction
int *pm; pm = &m;
*pm = 5; *pm = 5;
}
10
Dereferencing—Another Example
int C;
int *P; /* Declare P as a pointer to int */
C = 7;
P = &C;
cout<<*p<<endl; //What is the output?
C
… 7 3 4 …
172 173 174 175 176 177 178 179 180 181
P
… 174 3 4 …
832 833 834 835 836 837 838 839 840 841
Dereferencing
C
… 7 3
177 4 …
172 173 174 175 176 177 178 179 180 181
P
… 174 3
177 4 …
832 833 834 835 836 837 838 839 840 841
Pointers & Allocation (1/2)
• After declaring a pointer:
int *ptr1;
ptr1 doesn’t actually point to anything yet.
. We can either:
• make it point to something that already exists,
• ptr1 = &C
• or
ptr1 = NULL
• allocate room in memory for something new that it
will point to… (dynamic memory will discuss later)
Memory
Pointers & Allocation(2/2)
▪ var1 = 5;
▪ ptr = &var1;
ptr ? var1 5
? var2 5
?
Arithmetic on Pointers (1/4)
▪ Example 1
#include <iostream>
int main(){
int x;
int* ptr = &x ; // assume 4 byte ints
cout << ptr << '\n';
++ptr; // ptr = ptr + 1
cout << ptr << '\n’;
--ptr; // ptr = ptr - 1 Output:
cout << ptr << '\n’; 00AFFD70
00AFFD74
return 0; 00AFFD70
}
Arithmetic on Pointers (3/4)
Arithmetic on Pointers (4/4)
int main () {
int *pointer1, *pointer2;
int num1 = 93;
pointer1 = &num1; //address of num1
pointer2 = pointer1; // pointer1 address is assigned to
pointer2
cout<<“Address stored in pointer1 “<<pointer1;
cout<<“Address stored in pointer2 “<<pointer2;
}
Address data
1000
1004 num1 = 93
1008
Output:
3 4