0% found this document useful (0 votes)
57 views19 pages

Unit03 Pointers

Pointer variables store memory addresses. A pointer variable is declared with a data type followed by an asterisk, such as int *p. The address-of operator & returns the memory address of a variable. The dereference operator * accesses the value at the memory address a pointer points to. Pointers can point to dynamically allocated memory using new, which returns a pointer. Deep copying fully copies the values being pointed to, while shallow copying only copies the pointer values themselves.

Uploaded by

mohammad rjoob
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
57 views19 pages

Unit03 Pointers

Pointer variables store memory addresses. A pointer variable is declared with a data type followed by an asterisk, such as int *p. The address-of operator & returns the memory address of a variable. The dereference operator * accesses the value at the memory address a pointer points to. Pointers can point to dynamically allocated memory using new, which returns a pointer. Deep copying fully copies the values being pointed to, while shallow copying only copies the pointer values themselves.

Uploaded by

mohammad rjoob
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

Pointers

1
What is a Pointer

• Pointer variable: A variable whose content is an address (that is, a


memory address).

• In C++, you declare a pointer variable by using the asterisk symbol *


between the data type and the variable name.

dataType *identifier;

• Example:
int *p;
char *ch;

• p is called a pointer variable of type int


• ch is called a pointer variable of type char
2
What is a Pointer

• The following statements are equivalent:


int *p;
int* p;
int * p;

• Consider the following statement:


int* p, q;
– only p is a pointer variable
– Here q is an int variable

• To avoid confusion, attach the character * to the variable name:


int *p, q;

• Consider the following statement:


int *p, *q; // both p and q are pointer variables of type int 3
Address of Operator (&)

• In C++, the ampersand, &, called the address of operator

• 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 (*)

• When *, is used as a unary operator, * refers to the object to which


the operand of the * (that is, the pointer) points

• Example:
int x = 25;
int *p;
p = &x; //store the address of x in p

cout << *p << endl;


// prints the value stored in the memory space to which p points,
which is the value of x.

*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;

Consider the following statements:


1. num = 78;
2. p = &num;
3. *p = 24;

6
Initializing Pointer Variables

• Because C++ does not automatically initialize variables, pointer


variables must be initialized if you do not want them to point to
anything

• The following two statements are equivalent:


p = NULL;
p = 0;

• The number 0 is the only number that can be directly assigned to a


pointer variable.

7
Dynamic Variables

• Variables that are created during program execution are called


dynamic variables

• With the help of pointers, C++ creates dynamic variables

• C++ provides two operators:


– new: create dynamic variables, when a program requires a new
variable, the operator new is used
– delete: destroy dynamic variables, When a program no longer
needs a dynamic variable, the operator delete is used.

• In C++, new and delete are reserved words.

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

• Consider the following statements:


int *p; //p is a pointer of type int
p = new int; //allocates memory of type int and stores the
address of the allocated memory in p

9
Operator new

• Consider the following statements:


char *q;
q = new char[16];

// creates an array of 16 components of type char and


stores the base address of the array in q.

• Because a dynamic variable is unnamed, it cannot be accessed


directly. It is accessed indirectly by the pointer returned by new.
int *p;
p = new int;
*p = 28;

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

• Write a C++ code to declare any array of 10 items in dynamic


memory, fill with ones and then print all array items.

#include <iostream>
using namespace std;
int main()
{
int* a = new int[10];
for (int i = 0; i < 10; i++)
a[i] = 1;

for (int i = 0; i < 10; i++)


cout << a[i] << endl;

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;

Deep Copy Shallow Copy


All fields(members) of c1 are address value of pc1 is copied
copied to c2 fields to pc2 (pc2 points to same
place in memory to which pc1
In the memory, we have two points 2)
separate copies c1, c2
In the memory, we have only
18
one copy
Deep Copy vs Shallow Copy- Example 2
int *a; int *a;
a = new int[10]; a = new int[10];
int *b; int *b;
b = a;
b = new int[10];
for (int i=0; i<10 ; i++)
b[i] = a[i];

Deep Copy Shallow Copy

19

You might also like