Week 13
Week 13
Lecture Link:
https://www.youtube.com/watch?v=eUw-
yAtp4BQ&list=PLVEVLI2v6thVDz7UxUPnURUKaqWFK7Z7v&index=35
Pointer
// Wrong!
// &var is an address
// *varPoint is the value stored in &var
*varPoint = &var;
// Correct!
// varPoint is an address and so is &var
varPoint = &var;
// Correct!
// both *varPoint and var are values
*varPoint = var;
Pointer Arithmetic
int x =10 ;
int *yptr ;
yptr = &x ;
*yptr += 3 ;
yptr += 3 ;
Decrementing
*yptr --
Pointer Arithmetic
int *p1 ,*p2;
…..
p1 + p2 ;
Error
Pointers and Arrays
Lecture Link:
https://www.youtube.com/watch?v=VhryGzEe7sE&list=PLVEVLI2v6thVDz7UxUPnURUK
aqWFK7Z7v&index=36
Pointers and Arrays
In C++, Pointers are variables that hold addresses of
other variables. Not only can a pointer store the address
of a single variable, it can also store the address of cells
of an array.
return 0;
}
Pointers and Arrays
int y [ 10 ] ; Starting Address of Array y 0 [0]
1 [1]
2 [2]
3 [3]
4 [4]
5 [5]
6 [6]
7 [7]
8 [8]
9 [9]
The name of the array is like a
pointer which contain the
address of the first element.
location
3000 3004 3008 3012 3016
is same as
yptr = &y [ 0 ] ;
……..
yptr = &y [ 2 ] ;
Example to differentiate yptr
and *yptr
#include <iostream>
#include <string>
using namespace std;
int main() {
int x = 10 ;
int *yptr ;
yptr = &x ;
cout << yptr<<"\n" ;
cout << *yptr ;
}
pointer to a pointer