Lec #2
Lec #2
CS-201
LECTURE #2
Pointers
MAHMOOD AHMAD
Topics covered
Mahmood Ahmad
Addressing variables
Mahmood Ahmad
Addressing variables
• Each variable is assigned a memory slot (the size depends on the data type) and
the variable’s data is stored there
… … … …
a
Mahmood Ahmad
Addressing variables
• A pointer is a variable used to store the address of a memory cell.
• We can use the pointer to reference this memory cell
… … 100 … 1024 …
integer
pointer
Mahmood Ahmad
Pointer Types
• Pointer
C++ has pointer types for each type of object
Pointers to int objects
Pointers to char objects
Pointers to user-defined objects
• (e.g., RationalNumber)
Even pointers to pointers
Pointers to pointers to int objects
Mahmood Ahmad
Pointer Types
• A: It’s true that all addresses are of the same length, however when we
perform an operation of the type “p++” where “p” is a pointer variable, for
this operation to make sense the compiler needs to know the data type
of the variable “p” points to. If “p” is a character pointer then “p++” will
increment “p” by one byte (typically), if “p” were an integer pointer its
value on “p++” would be incremented by 2 bytes (typically).
Mahmood Ahmad
Pointer Types
Examples:
int *n;
RationalNumber *r;
int **p; // pointer to pointer
Mahmood Ahmad
Address Operator &
• The "address of " operator (&) gives the memory address of the
variable
Usage: &variable_name
… … … …
• a
• int a = 100;
• //get the value,
• cout << a; //prints 100
• //get the memory address
• cout << &a; //prints 1024
Mahmood Ahmad
Address Operator &
Memory address: 1020 1024 1032
… … … …
a b
#include <iostream>
Result is:
using namespace std;
The address of a is:
void main(){
1020
int a, b; The address of b is:
a = 88; 1024
b = 100;
cout << "The address of a is: " << &a << endl;
cout << "The address of b is: " << &b << endl;
}
Mahmood Ahmad
Address Operator &
… 88 100 … 1024 …
a p
int a = 100;
int *p = &a;
cout << a << " " << &a <<endl;
Result is:
cout << p << " " << &p <<endl;
100 1024
1024 1032
Mahmood Ahmad
Address Operator &
What is the
output?
58 58 58
Mahmood Ahmad
Dereferencing Operator *
• We can access to the value stored in the variable
pointed to by using the dereferencing operator (*),
… 88 100 … 1024 …
int a = 100; a p
int *p = &a; Result is:
cout << a << endl; 100
cout << &a << endl; 1024
cout << p << " " << *p << endl; 1024 100
cout << &p << endl; 1032
Mahmood Ahmad
Don’t get confused
• Declaring a pointer means only that it is a pointer:
int *p;
• Don’t be confused with the dereferencing operator,
which is also written with an asterisk (*). They are
simply two different tasks represented with the same
sign
int a = 100, b = 88, c = 8;
int *p1 = &a, *p2, *p3 = &c;
p2 = &b; // p2 points to b
p2 = p1; // p2 points to a Result is:
b = *p3; //assign c to b 888
*p2 = *p3; //assign c to a
cout << a << b << c;
Mahmood Ahmad
A Pointer Example
The code
x 9
a gets 18 p
Mahmood Ahmad
Another Pointer Example
#include <iostream>
using namespace std;
int main (){
int value1 = 5, value2 = 15 ;
int *p1, *p2;
p1 = &value1; // p1 = address of value1
p2 = &value2; // p2 = address of value2
*p1 = 10; // value pointed to by p1=10
*p2 = *p1; // value pointed to by p2= value
// pointed to by p1
p1 = p2; // p1 = p2 (pointer value copied)
*p1 = 20; // value pointed to by p1 = 20
cout << "value1==" << value1 << "/ value2==" << value2;
return 0;
}
Mahmood Ahmad
Reference Variables
A reference is an additional name to an existing
memory location
Pointer: Reference:
x 9 x
9
ref
ref
int x = 9;
int x=9;
int &ref = x;
int *ref;
ref = &x;
Mahmood Ahmad
Reference Variables
• A reference variable serves as an alternative name for an object
int m = 10;
int &j = m; // j is a reference variable
cout << “value of m = “ << m << endl;
j = 18;
cout << “value of m = “ << m << endl;
Output
value of m = 10
value of m = 18
Mahmood Ahmad
Reference Variables
• A reference variable always refers to the same object. Assigning a reference variable
with a new value actually changes the value of the referred object.
• Reference variables are commonly used for parameter passing to a function
Mahmood Ahmad
Traditional Pointer Usage
void IndirectSwap(char *Ptr1, char *Ptr2){
char temp = *Ptr1;
*Ptr1 = *Ptr2;
*Ptr2 = temp;
}
int main() {
char a = 'y';
char b = 'n';
IndirectSwap(&a, &b); Output
cout << a << b << endl; n
return 0; y
}
Mahmood Ahmad
Pass by Reference
void IndirectSwap(char& y, char& z) {
char temp = y;
y = z;
z = temp;
}
int main() {
char a = 'y';
char b = 'n';
Output
IndirectSwap(a, b); n
cout << a <<endl<< b << endl; y
return 0;
Mahmood Ahmad
Pass by Value
What is the Problem ??
1000
1004
1008
1012
1016
Mahmood Ahmad
Array Name is a pointer constant
#include <iostream>
using namespace std;
Result:
Address of a[0]:
0x0065FDE4
Name as pointer:
0x0065FDE4
Mahmood Ahmad
Dereferencing An Array Name
This element is
called a[0] or
*a
#include <iostream>
a[0] 2 using namespace std;
a void main(){
a[1] 4 int a[5] = {2,4,6,8,22};
a[2] 6 cout << *a << " "
<< a[0];
a[3] 8 } //main
a[4] 22
Output
a 2
2
Mahmood Ahmad
Array Names as Pointers
• To access an array, any pointer to the first element can be
used instead of the name of the array.
We could replace *p by *a
#include <iostream>
p using namespace std;
a
void main(){
a[0] 2 int a[5] = {2,4,6,8,22};
a[1] 4 int *p = a;
a[2] 6 cout << a[0] << " "
a[3] 8 << *p;
} Output
a[4] 22
2
a 2
Mahmood Ahmad
Multiple Array Pointers
• Both a and p are pointers to the same array.
#include <iostream>
a[0] using namespace std;
a[0] void main(){
2 p
4 int a[5] = {2,4,6,8,22};
a[1] 4 int *p = &a[1];
a[2] 6 cout << a[0] << " " Output
<< p[-1] << " “<< *( p-1) 2 2 2
a[3] 8 p[0]
cout << a[1] << " " 4 4 4
a[4] 22 << p[0] << " " << *p;
}
Mahmood Ahmad
Pointer Arithmetic
• Given a pointer p, p+n refers to the element that is offset
from p by n positions.
a 2 p - 1
a + 1 4 p
a + 2 6 p + 1
a + 3 8 p + 2
a + 4 22 p + 3
Mahmood Ahmad
Dereferencing Array Pointers
Mahmood Ahmad
NULL pointer
• NULL is a special value that indicates an empty pointer
• If you try to access a NULL pointer, you will get an error
int *p;
p = 0;
cout << p << endl; //prints 0
cout << &p << endl;//prints address of p
cout << *p << endl;//Error!
Mahmood Ahmad
Example
• char* a = “Hello”;
a -> gives address of ‘H’
*a -> gives ‘H’
a[0] -> gives ‘H’
a++ -> gives address of ‘e’
*a++ -> gives ‘e’
a = &b; where b is another char variable is
perfectly LEGAL. However “char a[100];” “a =&b;”
where b is another char variable is ILLEGAL.
Mahmood Ahmad
Pointer to 2-Dimensional Arrays
table[i] =
table
&table[i][0]
refers to
table + 1 table[ 0] or *( table + 0 ) the address
What is
of the ith
*table row
? table + 2 table[ 1] or *( table + 1 )
address
table[ 2] or *( table + 2 )
int table[3][4] = {{1,2,3,4}, *(table[i]+j)
What is {5,6,7,8},{9,10,11,12}}; = table[i][j]
**table
? for(int i=0; i<3; i++){
1 for(int j=0; j<4; j++)
cout << *(*(table+i)+j);
cout << endl;
Mahmood Ahmad
Pointers as Function Parameters
Mahmood Ahmad
Mahmood Ahmad