OOP - Lecture 3 Pointers, Arrays and DMA
OOP - Lecture 3 Pointers, Arrays and DMA
Spring 2022
Hira Naveed
Lecture # 3 Pointers and DMA
Casting pointers
int *pi;
double *pd;
pd = pi;
• Even if they are both the same size, C++ will still
give an error on implicit typecasting
Casting pointers
short *pd;
pd = (short*) pi;
cout<<*pd;
//Prints 0 (short size 2 bytes)
Pointer Arithmatic
int x = 25;
int *intptr = &x;
intptr++;
What happens now?
Pointer address increases by 4 bytes because the
pointer is of type int
Same as intptr + 1
6
Pointer Arithmatic
int x = 25;
int *intptr = &x;
intptr++;
7
Pointer Arithmatic
char x =‘4’;
char *charptr = &x;
charptr++;
What happens now?
Pointer address increases by 1 bytes because the
pointer is of type char
Same as charptr + 1
8
Pointer Arithmetic
9
Pointer Arithmatic
int x = 25;
int *intptr = &x;
11
Relationship Between Pointers and
Arrays
• Arrays and pointers are closely related
20
Pointer Arithmetic
• Operations on pointer variables:
Operation Example
int vals[]={4,7,11};
int *valptr = vals;
int A[25];
int *p; int i=0, j=0;
p = A;
p points to A[0]
p + i points to A[i]
&A[j]is also the same as p+j
A[j] is also the same as *(p+j)
Arrays Decay to Pointers
showValues(numbers, SIZE);
Arrays passed by
Arrays Decay to Pointersvalue decay into a
pointer.
Cannot find size using
The values parameter, in the
sizeof() and must pass
showValues function, points to the
size as a function
numbers array.
parameter
{ 1004 Element 6
…
}
…
… Element 49
This is Equivalent to
for ( int index = 0; index < 50; index++ )
cout << Array31
[ index ] ;
Accessing 2-Dimensional Array
Address Data
• Note that the statements 980 Element 0
984 Element 1
int *Pointer;
988 Element 2
Pointer = &List [3];
992 Element 3
• represents that we are accessing the address 996 Element 4
of 4th slot 1000 Element 5
1004 Element 6
• In 2-Dimensional array the statements 1008 Element 7
int List[ 5 ][ 6 ]; 1012 Element 8
int *Pointer; …
Pointer = &List [3];
…
… Element 49
Represents that we are accessing the address
of 4th row and 1st column
Accessing 2-Dimensional Array
Column
• int List [ 9 ] [ 6 ]; 0 1 2 3 4 5
Row
1 312 314 316 318 320 322
• ptr = &List [3];
2 324 326 328 330 332 334
3 336 338 340 342 344 346
• To access the address of 4th row 2nd 4 348 350 352 354 356 358
column: 5
6
360 362 364 366 368 370
372 374 376 378 380 382
• ptr++; // address of 4 row 2
th nd
7 384 386 388 390 392 394
column 8 396 398 400 402 404 406
5 rows * 4
columns = 20
elements
Approach:
• Allocate 20 elements using dynamic allocation
• Use a pointer to point and access those items
Accessing 2-Dimensional Array
//equivalent to arr[1][1]
cout<<*(p +(3 * 1)+ 1); //prints 5
//equivalent to arr[2][2]
cout<<*(p +(3 * 2)+ 2); //prints 9
Accessing 2-Dimensional Array
//equivalent to arr[1][1]
cout<<*(p +(3 * 1)+ 1); //prints 5
//equivalent to arr[2][2]
cout<<*(p +(3 * 2)+ 2); //prints 9
Dynamic 2D Arrays
Dynamic 2D Array – Double Pointer
2. Using a Pointer that points to an Array of Pointer
• Total elements in a 2D Array:
M*N i.e (rows * columns)
N
Ptr2D
(Pointer to a
Pointer a.k.a
double pointer)
M
Dynamic 2D Array – Double Pointer
arr start of array of pointers
int M=3, N=4; *arr First Address pointed by first row (sub array)
*(*arr) First value of first array
(*arr)++ Move to next address in the first array
// Dynamically create array of pointers of size M
arr++ Move to Next row (second array address)
Can we vary the size of
int **arr = new int*[M];
each column in
Dynamic 2D Array
(using double
// Dynamic allocate memory pointer)
of size N for each row
for(int i = 0; i < M; i++){
arr[i] = new int[N] **
}
// deallocate memory *
for(int i = 0; i < M; i++){ *
delete[] arr[i]; } *
delete[] arr;
Dynamic 2D
Array
(Varying Row Size)
Dynamic 3D Array
Manipulating a 3D Array
1. Using one pointer
2. Using a triple pointer
3D Array Using
a single pointer
3D Array Using a
triple pointer
***
3D array of size 2 x 3x4
** **
* *
* *
* *
C - Strings
Stores
strings as
char arrays
C - Strings
B a i l e y \0
0 1 2 3 4 5 6
\0 is the NULL character
Character Arrays
B a i l e y y
0 1 2 3 4 5 6
Character Arrays
B a i l \0 \0 \0
0 1 2 3 4 5 6
Character Arrays
B a i l e y \0
0 1 2 3 4 5 6
Character Arrays
B a i l \0
0 1 2 3 4
Character Arrays
B a i l \0
0 1 2 3 4
Character Arrays
B a i l
0 1 2 3
Character Arrays
B a i l
0 1 2 3
Character Arrays and Char Pointer
***
**
char arr[] = "****";
int i = 1; *
for (; i < 3; i++) { **
cout << (arr+i) << endl; **
*
}
for (; i >= 1; i--) {
cout << (arr + i) << endl;
}
Character Arrays and Char Pointer
• cout prints characters from the pointer address till the null
character
B a i l \0
0 1 2 3 4
Character Arrays and Char Pointer
B a i l \0
0 1 2 3 4
Character Arrays and Char Pointer
void match(char* str, char ch, int size)
{
for(int i=0;i<size;i++){
if(*str == ch){
cout << str;
break; e string
}
else
str++; }}
void main() {
char arr[] = "some string";
match(arr,'e',sizeof(arr));
}
Character Arrays and Char Pointer
// Copying string using Pointers
char* str1 = “Self-conquest is the greatest
victory.”;
char str2[80]; //empty string
char* src = str1;
char* dest = str2;
cin.getline(name, SIZE);
If you are using cin and cin.getline together, you would have
to clear the input buffer inbetween
int x, arr[3];
cin>>x;
cin.ignore(); //clears the input stream
buffer
cin.getline(arr,3);
Pointers to Constants
const int a = 5;
int *foo = &a;
ERROR!!
const int a = 5;
Be careful:
const is applied to the thing that foo points to, not
foo itself
const int a = 5;
const int *foo= &a;
const int a = 5;
const int *foo= &a;
*foo = NULL;
const int a = 5;
const int *foo= &a;
• Example
• Example:
const int value = 22;
const int * const ptr = &value;
Constant Pointers to Constants
Constant Pointers to Constants
• Example:
int value = 22;
const int * const ptr = &value;
int num = 7;
getNum(&num); //function call
Pointers as Function Parameters
int gl = 93;
void func(int *num) Before: n = 5
{ After: n = 5
num = ≷
}
void main()
{ int n = 5; int *p = &n;
cout<<"Before: n = "<<n<<endl;
func(p);
cout<<"After: n = "<<n<<endl;
}
Pointers as Function Parameters
}
Reference to Pointer Function
Parameter
• Recall reference parameters
20 =x
void fun(int &ref) { = ref
ref = 20;
} The reference parameter
ref is just another name
void main() {
for x. Like an alias/nickname
int x = 10;
fun(x);
cout<<"New value of x is "<< x;
//prints 20
}
Reference to Pointer Function
Parameter
• Recall reference parameters
void fun(int &ref) { Modifications in
ref = 20; variables passed by
} reference are
retained, even after
void main() { function returns
int x = 10;
fun(x);
cout<<"New value of x is "<< x;
//prints 20
}
Reference to Pointer Function
Parameter
int gl = 95;//address 1500 n=5 2000
void func(int *&num)
{ ptr = 2000 2400
num = ≷
}
void main()
{ int n = 5; int *ptr = &n;
cout<<"Before = "<< *ptr <<endl;
func(ptr);
cout<<"After = "<< *ptr <<endl;
}
Reference to Pointer Function
Parameter
int gl = 95;//address 1500 n=5 2000
void func(int *&num)
{ ptr = 2000 2400
num =
num = ≷
} num is just another
void main() name for ptr.
{ int n = 5; int *ptr = &n;
cout<<"Before = "<< *ptr <<endl;
func(ptr);
cout<<"After = "<< *ptr <<endl;
}
Reference to Pointer Function
Parameter
int gl = 95;//address 1500 n=5 2000
void func(int *&num)
{ ptr = 1500 2400
Before = 5 num =
num = ≷
} After = 95
num is just another
void main() name for ptr.
{ int n = 5; int *ptr = &n;
cout<<"Before = "<< *ptr <<endl;
func(ptr);
cout<<"After = "<< *ptr <<endl;
}
Pointer to Pointer(**) vs Reference to
Pointer(*&)
• Reference is just another name(alias), pointer is a
variable that stores an address
107
int main() {
// use the following dimensions of an array
int N = 4;
int M = 4;
// Write code for dynamic allocation of a 2D array named A,
using a double pointer[1 marks]
int** A = new int* [N];
for (int i = 0; i < N; i++) {
A[i] = new int[M];
}
// Assume that you have populated the array as following.
for (int i = 0; i < N; ++i) {
for (int j = 0; j < M; ++j) {
A[i][j] = i + 5;
}
}
108
//Use pointer arithmetic and print the output of the following
statements [5 marks]
//Assume starting address of this dynamically allocated array of
pointers is 100
cout << **A << endl; // write your output here: 5
cout << *(*A + 2) + 1 << endl; // write your output here: 6
cout << **A + 100 << endl; // write your output here: 105
cout << *(*(A + 2) + 3) + 5 << endl; // write your output here: 12
cout << *A + 3;// write your output here: starting address of first
integer array + 12 bytes
110
(a) Given the following code snippet, create a pointer to the given
pointer (ptr) named ptr_to_ptr, then print the value stored in
variable ‘x’ using this newly created ptr_to_ptr: [1 Mark]
int x = 10;
int **ptr_to_ptr=&ptr;
cout<<**ptr_to_ptr;
(b) Given the following code snippet, what will be the output? [1
Marks]
int x = 100;
int y = 200;
p = q;
cout<<*p; 200
111
(c) Given the following code snippet, what will be the output? [1
Marks]
char arr[20];
int i;
*(arr + i) = ‘\0’;
ABCDEFGHIJ
112
(d) Given the following code snippet, what will be the output? [2
Marks]
char *ptr;
ptr = arr;
ptr + = 5;
fgh
(e) Given the following code snippet, what will be the output?
(Assuming memory address of variable ‘x’ is 01434CC3.) [2 Marks]
int x = 50;
ptr = ptr + 1;