Object Oriented Programming
Object Oriented Programming
Lecture 2A – Pointers
Ubaid Ur Rehman
[email protected]
Outline
Pointer and Address Operators
Pointer Variables
Dereferencing
Pointers and Arrays
Pointer Arithmetic
int i; 0x1054
int *ptr; 17
ptr=&i;
#include <iostream>
using namespace std;
int main()
{
int x = 25; //x is store on address 0x7e00
int *ptr;
The value in x is 25
The address of x is 0x7e00
x
25
ptr
0x7e00
Address of x: 0x7e00
Exercise
// This program demonstrates the use of the indirection
operator.
#include <iostream>
using namespace std;
int main()
{
int x = 25;
int *ptr;
ptr = &x; // Store the address of x in ptr
cout << "Here is the value in x, printed twice:\n";
cout << x << " " << *ptr << endl;
*ptr = 100;
cout << "Once again, here is the value in x:\n";
cout << x << " " << *ptr << endl;
}
Exercise: Output
Output:
Dereferencing: Example
#include <iostream>
using namespace std; ptr FFF0 FFF4
void main()
{ FFF1
float data = 50.8; FFF2
float *ptr;
FFF3
ptr = &data;
cout << ptr << *ptr << endl; data FFF4 50.8
*ptr = 27.4; FFF5
cout << *ptr << endl;
cout << data << endl; FFF6
}
Output:
FFF450.8
Dereferencing: Example
#include <iostream>
using namespace std; ptr FFF0 FFF4
void main()
{ FFF1
float data = 50.8; FFF2
float *ptr;
FFF3
ptr = &data;
cout << ptr << *ptr << endl; data FFF4 27.4
*ptr = 27.4; FFF5
cout << *ptr << endl;
cout << data << endl; FFF6
}
Output:
Dereferencing: Example
#include <iostream>
using namespace std;
void main() ptr FFF0 FFF4
{ FFF1
float data = 50.8;
float *ptr; FFF2
ptr = &data; FFF3
cout << ptr << *ptr << endl;
*ptr = 27.4; data FFF4 27.4
cout << *ptr << endl; FFF5
cout << data << endl;
FFF6
}
Output:
FFF450.8
27.4
Dereferencing: Example
#include <iostream>
using namespace std;
void main() ptr FFF0 FFF4
{ FFF1
float data = 50.8;
float *ptr; FFF2
ptr = &data; FFF3
cout << ptr << *ptr << endl;
*ptr = 27.4; data FFF4 27.4
cout << *ptr << endl; FFF5
cout << data << endl;
FFF6
}
Output: FFF450.8
27.4
27.4
Pointers and Arrays
Given:
int vals[]={4,7,11};
int *valptr = vals;
What is valptr + 1?
It means (address in valptr) + (1 * size of an int)
cout << *(valptr+1); // displays 7
cout << *(valptr+2); // displays 11
Must use ( ) in expression
Example
// This program shows an array name being dereferenced
with the * operator.
#include <iostream>
using namespace std;
int main()
{
short numbers[] = {10, 20, 30, 40, 50};
numbers
Exercise
// This program processes the contents of an array. Pointer
notation is used.
#include <iostream>
using namespace std;
int main()
{
int numbers[5];
cout << "Enter five numbers: ";
for (int count = 0; count < 5; count++)
cin >> *(numbers + count);
cout << "Here are the numbers you entered:\n";
for (int count = 0; count < 5; count++)
cout << *(numbers + count)<< " ";
cout << endl;
}
Exercise: Output
0EC4
0EC5
0EC6
0EC7
Dynamic Memory Allocation:
Example
int *ptr; ptr FDE0 0EC4
ptr = new int; FDE1
*ptr = 22; FDE2
cout << *ptr << endl;
FDE3
delete ptr;
ptr = NULL;
0EC4
0EC5
0EC6
0EC7
Dynamic Memory Allocation:
Example
int *ptr; ptr FDE0 0EC4
ptr = new int; FDE1
*ptr = 22; FDE2
cout << *ptr << endl;
FDE3
delete ptr;
ptr = NULL;
0EC4 22
0EC5
0EC6
0EC7
Dynamic Memory Allocation:
Example
int *ptr; ptr FDE0 0EC4
ptr = new int; FDE1
*ptr = 22; FDE2
cout << *ptr << endl;
FDE3
delete ptr;
ptr = NULL;
0EC4 22
Output:
0EC5
22 0EC6
0EC7
Dynamic Memory Allocation:
Example
int *ptr; ptr FDE0 ?
ptr = new int; FDE1
*ptr = 22; FDE2
cout << *ptr << endl;
FDE3
delete ptr;
ptr = NULL;
0EC4
0EC5
0EC6
0EC7
Dynamic Memory Allocation:
Example
int *ptr; ptr FDE0 0
ptr = new int; FDE1
*ptr = 22; FDE2
cout << *ptr << endl;
FDE3
delete ptr;
ptr = NULL;
0EC4
0EC5
0EC6
0EC7
Dynamic Memory Allocation