Unit03 Pointers
Unit03 Pointers
1
What is a Pointer
dataType *identifier;
• Example:
int *p;
char *ch;
• Example:
int x;
int *p;
p = &x; // assigns the address of x to p.
– x and the value of p refer to the same memory location.
4
Dereferencing Operator (*)
• Example:
int x = 25;
int *p;
p = &x; //store the address of x in p
*p = 55;
// stores 55 in the memory location to which p points
—that is, 55 is stored in x.
5
Dereferencing Operator (*)
• Example:
int *p;
int num;
6
Initializing Pointer Variables
7
Dynamic Variables
8
Operator new
• The operator new has two forms: one to allocate a single variable,
and another to allocate an array of variables.
• Syntax
new dataType; //to allocate a single variable
new dataType[intExp]; //to allocate an array of variables
intExp : any expression evaluating to a positive integer.
• The operator new allocates memory (a variable) of the designated
type and returns a pointer to it
9
Operator new
10
Example 1: What is the output?
#include <iostream>
using namespace std;
int main() x = 50
{ *p = 50
int* p; x = 50
int x = 50; *p = 100
cout << "x = " << x << endl; x = 100
p = &x;
address of p = 0000003D2C0FF584
cout << "*p = " << * p << endl;
cout << "x = " << x << endl; address of x = 0000003D2C0FF584
*p = 100;
cout << "*p = " << *p << endl;
cout << "x = " << x << endl;
cout << "address of p = " << p << endl;
cout << "address of x = " << &x << endl;
}
11
Example 2: What is the output?
#include <iostream>
using namespace std;
int main() 13
{ 13
int a = 1, b = 2, c = 3;
int* p, * q;
p = &a;
q = &b;
p = q;
*p = 13;
cout << *p << endl;
cout << *q << endl;
}
12
Example 3: What is the output?
#include <iostream>
using namespace std;
2
void f1(int* ptr) { 4
*ptr = *ptr + 2;
}
int main()
{
int x = 2;
int* p = &x;
cout << x << endl;
f1(p);
cout << x << endl;
}
13
Example 4: What is the output?
#include <iostream>
using namespace std;
int main() 5
{ 2
int* q = new int;
*q = 5;
cout << *q << endl;
int x = 2;
int* p = new int(2);
cout << *p << endl;
delete(p);
p = NULL;
14
Example 5: Write a code
#include <iostream>
using namespace std;
int main()
{
int* a = new int[10];
for (int i = 0; i < 10; i++)
a[i] = 1;
15
Pointers to struct
struct circle {
Notes:
double x, y, radius;
}; pc.x not valid (pc is a pointer)
circle c; *pc.x not valid ( dot(.) has higher priority)
circle* pc; // pointer of type
struct circle
c.x = 5.0;
c.y = 7.4; We better use ->
(member access operator arrow)
c.radius = 8.6;
(*pc).x = 3.3; is equivalent to
pc->x = 3.3;
pc = new circle; /* allocate space for
a struct of type circle */
(*pc).radius = 6.3; pc->radius = 6.3;
(*pc).x = 3.3; pc->x = 3.3;
(*pc).y = 2.3; pc->y = 2.3;
16
Comparing Pointers
int* p, * q;
p = new int; p == q ? False
*p = 50; *p ==*q? True
q = new int;
*q = 50;
int* p, * q;
*p = 50; p == q ? True
q = p; *p ==*q? True
17
Deep Copy vs Shallow Copy- Example 1
struct circle { struct circle {
double x, y, r; double x, y, r;
}; };
circle *pc1, *pc2;
circle c1, c2; pc1 = new circle;
c1.x = 5.6; pc1->x = 5.36;
c1.y = 3.99; pc1->y = 8.14;
c1.r = 4.33; pc1->r = 5.66;
c2 = c1; pc2 = pc1;
19