0% found this document useful (0 votes)
15 views20 pages

Lecture 3a - Pointers in C++ Spring 2025

This lecture covers the fundamentals of pointers in C++, including their declaration, initialization, dereferencing, and arithmetic operations. It contrasts regular variables with pointer variables, emphasizing that pointers hold memory addresses rather than values. Additionally, it addresses common errors in pointer usage and includes class activities to reinforce learning.

Uploaded by

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

Lecture 3a - Pointers in C++ Spring 2025

This lecture covers the fundamentals of pointers in C++, including their declaration, initialization, dereferencing, and arithmetic operations. It contrasts regular variables with pointer variables, emphasizing that pointers hold memory addresses rather than values. Additionally, it addresses common errors in pointer usage and includes class activities to reinforce learning.

Uploaded by

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

Object Oriented Programming

Lecture 3

C++ Pointers
Spring 2025
Lecture Contents

▪ Introduction to Pointers

▪ Pointer variable declaration

▪ 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

▪ Every location in memory has an address


▪ Use address-of operator ‘&’ to get the address
▪ Example:

cout << "Address of myInt = " << &myInt ;

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

• An address must be assigned to the pointer variable


before it can be used

int myInt = 0; //Allocates memory, stores 0

int *pMyInt; //Declares an empty pointer

pMyInt = &myInt; //Puts an address in the 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).

3. Content at the memory address 174 is 7


2. myInt’s memory address
is 174

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;

int n; ERROR! Missing operator*


int *pn = &n;
//Correction
pn = 5; *pn = 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

cout << *P; /* Prints out ‘7’ */


*P = 177;
P = 177; /* This is unadvisable!! */

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)

▪ Pointing to something that already exists:

▪ int *ptr, var1, var2;

▪ var1 = 5;

▪ ptr = &var1;

▪ var2 = *ptr; //Dereferencing using the * opetator

▪ var1 and var2 have room implicitly allocated for them.

ptr ? var1 5
? var2 5
?
Arithmetic on Pointers (1/4)

▪ A pointer may be incremented or decremented.

▪ This means only address that the pointer holds is incremented or


decremented.

▪ An integer may be added to or subtracted from a pointer.

▪ Pointer variables may be subtracted from one another.

▪ Pointer variables can be used in comparisons, but usually only in


a comparison to pointer variables or NULL.
Arithmetic on Pointers (2/4)
▪ When an integer (n) is added to or subtracted from a pointer (ptr)
▪ The new pointer value (ptr) is changed by the ptr address plus (+) n
multiple (x*) the (bytes of ptr data type).

▪ ptr + n * (bytes of ptr data type)

▪ 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

1012 pointer1 = 1004

1016 pointer2 = 1004


Logical operators on Pointers (<,>,== etc.)
• Example
int *pointer1, *pointer2;
int num1 = 93;
If ( pointer1 == NULL ) // pointer compared to NULL
pointer1 = &num1;
pointer2 = &num1;
If ( pointer1 == pointer2 ) // pointer compared to
pointer
cout<<“Both pointers are equal\n”;
Class activity
• What is the output of the below code?
int i = 1;
int j = 2;
int *ptr;
ptr = &I;
*ptr = 3;
ptr = &j;
*ptr = 4;
cout<< i << “ “<<j<<endl;
Class activity
• What is the output of the below code?
int i = 1;
int j = 2;
int *ptr;
ptr = &i;
*ptr = 3;
ptr = &j;
*ptr = 4;
cout<< i << “ “<<j<<endl;

Output:
3 4

You might also like