Pointers Review
Pointers Review
Pointers
100 1024
integer pointer
COMP104 Pointers / Slide 3
Computer Memory
A variable is in fact a portion of memory to
store a determined value
Each variable is assigned a memory slot (the
size depends on the data type) and the
variables data is stored there
100 1024
a
Variable as value, i.e., 100, is
int a = 100; stored at memory location 1024
COMP104 Pointers / Slide 4
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
COMP104 Pointers / Slide 5
100
a
int a = 100;
//To get the value, use the variable name
cout << a; //prints 100
//To get the memory address, add the address
//operator before the variable name
cout << &a; //prints 1024
COMP104 Pointers / Slide 6
88 100
a b
#include <iostream>
using namespace std;
Result is:
void main(){ The address of a is: 1020
int a, b; The address of b is: 1024
a = 88;
b = 100;
cout << "The address of a is: " << &a << endl;
cout << "The address of b is: " << &b << endl;
}
COMP104 Pointers / Slide 7
Pointer Variable
Pointer Variables
Memory address: 1020 1024 1032
88 100 1024
a p
int a = 100; Result is:
int *p = &a; 100 1024
cout << a << " " << &a <<endl; 1024 1032
cout << p << " " << &p <<endl;
Reference Operator *
We can access to the value stored in the variable
pointed to by preceding the pointer with the star
reference operator (*),
Memory address: 1020 1024 1032
88 100 1024
a p
int a = 100;
int *p = &a; Result is:
cout << a << endl; 100
cout << &a << endl; 1024
cout << p << " " << *p << endl; 1024 100
cout << &p << endl; 1032
COMP104 Pointers / Slide 10
Pointer Example
#include <iostream> Result is
using namespace std;
value1==10 / value2==20
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;
}
COMP104 Pointers / Slide 12
Pass by Reference
void IndirectSwap(char& y, char& z) {
char temp = y;
y = z;
z = temp;
}
int main() {
char a = 'y';
char b = 'n';
IndirectSwap(a, b);
cout << a << b << endl;
return 0;
}
COMP104 Pointers / Slide 15
Alternative
Pass by Reference
void IndirectSwap(char &y, char &z) {
char temp = y;
y = z;
z = temp;
}
int main() {
char a = 'y';
char b = 'n';
IndirectSwap(a, b);
cout << a << b << endl;
return 0;
}
COMP104 Pointers / Slide 16
Pointer to Pointer
58 58 58
COMP104 Pointers / Slide 17
int *p = &a;
int **q = &p;
int ***r = &q;
int ****s = &r;
q = &a //illegal!
s = &q //illegal!
COMP104 Pointers / Slide 18
1000
1004
1008
1012
1016
COMP104 Pointers / Slide 19
/* result:
Address of a[0]: 0x0065FDE4
Name as pointer: 0x0065FDE4
*/
COMP104 Pointers / Slide 20
This element is
called a[0] or
*a #include <iostream>
using namespace std;
a[0] void main(){ 222
2
int a[5] = {2,4,6,8,22};
a[1] 4 a cout << *a << " "
a[2] << a[0] << " "
6
<< *(&a[0]);
a[3] 8 } //main
a[4] 22
a
COMP104 Pointers / Slide 21
#include <iostream>
using namespace std;
a p void main(){
int a[5] = {2,4,6,8,22}; 22
a[0] 2
int *p = a;
a[1] 4
int i = 0;
a[2] 6
cout << a[i] << " "
a[3] 8 << *p;
a[4] 22 }
a
COMP104 Pointers / Slide 22
#include <iostream>
A[0] using namespace std;
a[0] 22 void main(){
2 p
44 int a[5] = {2,4,6,8,22};
a[1] 4 int *p = &a[1];
a[2] 6 cout << a[0] << " "
<< p[-1];
a[3] 8 P[0]
cout << a[1] << " "
a[4] 22 << p[0];
}
COMP104 Pointers / Slide 23
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
COMP104 Pointers / Slide 24
a[0] or *(a + 0) 2 a
a[1] or *(a + 1) 4 a + 1
a[2] or *(a + 2) 6 a + 2
a[3] or *(a + 3) 8 a + 3
a[4] or *(a + 4) 22 a + 4
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!
COMP104 Pointers / Slide 27
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
? for(int i=0; i<3; i++){
for(int j=0; j<4; j++)
cout << *(*(table+i)+j);
cout << endl;
}