0% found this document useful (0 votes)
31 views

Ch12 Pointers

The document summarizes pointers in C++, including: declaring pointer variables; using the address of (&) and dereference (*) operators; pointers with classes/structs; dynamic variables using new/delete; pointer arithmetic; dynamic arrays; and pointers with functions. The key topics are pointers store memory addresses; dynamic memory is allocated using new and deleted using delete; and pointers allow accessing dynamic variables and arrays created at runtime.

Uploaded by

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

Ch12 Pointers

The document summarizes pointers in C++, including: declaring pointer variables; using the address of (&) and dereference (*) operators; pointers with classes/structs; dynamic variables using new/delete; pointer arithmetic; dynamic arrays; and pointers with functions. The key topics are pointers store memory addresses; dynamic memory is allocated using new and deleted using delete; and pointers allow accessing dynamic variables and arrays created at runtime.

Uploaded by

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

Chapter 12:

Pointers
Objectives
• In this chapter, you will:
– Learn about the pointer data type and pointer
variables
– Explore how to declare and manipulate pointer
variables
– Learn about the address of the operator and the
dereferencing operator
– Learn how pointers work with classes and structs
– Discover dynamic variables

C++ Programming: From Problem Analysis to Program Design, Seventh Edition 2


Objectives (cont’d.)
– Explore how to use the new and delete operators to
manipulate dynamic variables
– Learn about pointer arithmetic
– Learn how to work with dynamic arrays
– Become familiar with the limitations of range-based
for loops with dynamic arrays
– Explore how pointers work with functions as
parameters and functions as return values
– Become familiar with the shallow and deep copies of
data
C++ Programming: From Problem Analysis to Program Design, Seventh Edition 3
Pointer Data Type and Pointer
Variables
• Pointer variable: content is a memory address
• No name associated with the pointer data
type in C++

C++ Programming: From Problem Analysis to Program Design, Seventh Edition 4


Declaring Pointer Variables
• Syntax:

• Examples:
int *p;
char *ch;
• These statements are equivalent:
int *p;
int* p;
int * p;
C++ Programming: From Problem Analysis to Program Design, Seventh Edition 5
Declaring Pointer Variables (cont’d.)
• In the statement:
int* p, q;
– Only p is a pointer variable
– q is an int variable

• To avoid confusion, attach the character * to


the variable name:
int *p, q;
int *p, *q;
C++ Programming: From Problem Analysis to Program Design, Seventh Edition 6
Address of Operator (&)
• Address of operator (&):
– A unary operator that returns the address of its
operand
• Example:
int x;
int *p;
p = &x;
– Assigns the address of x to p

C++ Programming: From Problem Analysis to Program Design, Seventh Edition 7


Address of Operator (&)

C++ Programming: From Problem Analysis to Program Design, Seventh Edition 8


Address of Operator (&)

C++ Programming: From Problem Analysis to Program Design, Seventh Edition 9


Address of Operator (&)

C++ Programming: From Problem Analysis to Program Design, Seventh Edition 10


Address of Operator (&)

C++ Programming: From Problem Analysis to Program Design, Seventh Edition 11


Address of Operator (&)

C++ Programming: From Problem Analysis to Program Design, Seventh Edition 12


Dereferencing Operator (*)
• Dereferencing operator (or indirection
operator):
– When used as a unary operator, * refers to object
to which its operand points
• Example:
cout << *p << endl;
– Prints the value stored in the memory location
pointed to by p

C++ Programming: From Problem Analysis to Program Design, Seventh Edition 13


Classes, structs, and Pointer
Variables
• You can declare pointers to other data types:

– student is an object of type studentType


– studentPtr is a pointer variable of type
studentType
C++ Programming: From Problem Analysis to Program Design, Seventh Edition 14
Classes, structs, and Pointer
Variables (cont’d.)
• To store address of student in
studentPtr:
studentPtr = &student;
• To store 3.9 in component gpa of student:
(*studentPtr).gpa = 3.9;
– ( ) used because dot operator has higher
precedence than dereferencing operator
– Alternative: use member access operator arrow (-
>)

C++ Programming: From Problem Analysis to Program Design, Seventh Edition 15


Classes, structs, and Pointer
Variables (cont’d.)
• Syntax to access a class (struct) member
using the operator -> :

• Thus,
(*studentPtr).gpa = 3.9;
is equivalent to:
studentPtr->gpa = 3.9;

C++ Programming: From Problem Analysis to Program Design, Seventh Edition 16


Initializing Pointer Variables
• C++ does not automatically initialize variables
• Pointer variables must be initialized if you do
not want them to point to anything
– Initialized using the null pointer: the value 0
– Or, use the NULL named constant
– The number 0 is the only number that can be
directly assigned to a pointer variable
• C++11 includes a nullptr

C++ Programming: From Problem Analysis to Program Design, Seventh Edition 17


Dynamic Variables
• Dynamic variables: created during execution
• C++ creates dynamic variables using pointers
• new and delete operators: used to create
and destroy dynamic variables
– new and delete are reserved words in C++

C++ Programming: From Problem Analysis to Program Design, Seventh Edition 18


Operator new
• new has two forms:

– intExp is any expression evaluating to a positive


integer
• new allocates memory (a variable) of the
designated type and returns a pointer to it
– The allocated memory is uninitialized

C++ Programming: From Problem Analysis to Program Design, Seventh Edition 19


Operator new (cont’d.)
• Example: p = new int;
– Creates a variable during program execution
somewhere in memory
– Stores the address of the allocated memory in p
• To access allocated memory, use *p
• A dynamic variable cannot be accessed
directly
• Because it is unnamed

C++ Programming: From Problem Analysis to Program Design, Seventh Edition 20


Operator delete
• Memory leak: previously allocated memory that
cannot be reallocated
– To avoid a memory leak, when a dynamic variable is no
longer needed, destroy it to deallocate its memory
• delete operator: used to destroy dynamic
variables
• Syntax:

C++ Programming: From Problem Analysis to Program Design, Seventh Edition 21


Operator delete

C++ Programming: From Problem Analysis to Program Design, Seventh Edition 22


Operator delete

C++ Programming: From Problem Analysis to Program Design, Seventh Edition 23


Operator delete

C++ Programming: From Problem Analysis to Program Design, Seventh Edition 24


Operations on Pointer Variables
• Assignment: value of one pointer variable can
be assigned to another pointer of same type
• Relational operations: two pointer variables of
same type can be compared for equality, etc.
• Some limited arithmetic operations:
– Integer values can be added and subtracted from
a pointer variable
– Value of one pointer variable can be subtracted
from another pointer variable

C++ Programming: From Problem Analysis to Program Design, Seventh Edition 25


Operations on Pointer Variables
(cont’d.)
• Pointer arithmetic can be very dangerous:
– Program can accidentally access memory
locations of other variables and change their
content without warning
• Some systems might terminate the program with an
appropriate error message
• Always exercise extra care when doing pointer
arithmetic

C++ Programming: From Problem Analysis to Program Design, Seventh Edition 26


Dynamic Arrays
• Dynamic array: array created during program
execution
• Example:
int *p;
p = new int[10];

*p = 25; stores 25 into the first memory location


p++; //to point to next array component
*p = 35; stores 35 into the second memory location

C++ Programming: From Problem Analysis to Program Design, Seventh Edition 27


Dynamic Arrays (cont’d.)
• Can use array notation to access these
memory locations
• Example:
p[0] = 25;
p[1] = 35;
– Stores 25 and 35 into the first and second array
components, respectively

C++ Programming: From Problem Analysis to Program Design, Seventh Edition 28


Functions and Pointers
• Pointer variable can be passed as a parameter
either by value or by reference
• As a reference parameter in a function
heading, use &:
void pointerParameters(int &p, double *q)
{
. . .
}

C++ Programming: From Problem Analysis to Program Design, Seventh Edition 29


Pointers and Function Return Values
• A function can return a value of type pointer:

int* testExp(...)
{
. . .
}

C++ Programming: From Problem Analysis to Program Design, Seventh Edition 30


Dynamic Two-Dimensional Arrays
• You can create dynamic multidimensional
arrays
• Examples:
declares board to be an array of four
pointers wherein each pointer is of type int

creates the rows of board


declares board to be a pointer to a pointer

C++ Programming: From Problem Analysis to Program Design, Seventh Edition 31


Dynamic Two-Dimensional Arrays

C++ Programming: From Problem Analysis to Program Design, Seventh Edition 32


Shallow Versus Deep Copy and
Pointers
• Shallow copy: when two or more pointers of
the same types point to the same memory
– They point to the same data
– Danger: deleting one deletes the data pointed to
by all of them

C++ Programming: From Problem Analysis to Program Design, Seventh Edition 33


Shallow Versus Deep Copy and
Pointers

C++ Programming: From Problem Analysis to Program Design, Seventh Edition 34


Shallow Versus Deep Copy and
Pointers

C++ Programming: From Problem Analysis to Program Design, Seventh Edition 35


Shallow Versus Deep Copy and
Pointers
• Deep copy: when the contents of the memory
pointed to by a pointer are copied to the
memory location of another pointer
– Two copies of the data

C++ Programming: From Problem Analysis to Program Design, Seventh Edition 36


Shallow Versus Deep Copy and
Pointers

C++ Programming: From Problem Analysis to Program Design, Seventh Edition 37


Shallow Versus Deep Copy and
Pointers

C++ Programming: From Problem Analysis to Program Design, Seventh Edition 38


Summary
• Pointer variables contain the addresses of
other variables as their values
• Declare a pointer variable with an asterisk, *,
between the data type and the variable
• Address of operator (&)returns the address of its
operand
• Unary operator * is the dereferencing operator
• Member access operator (->) accesses the object
component pointed to by a pointer

C++ Programming: From Problem Analysis to Program Design, Seventh Edition 39


Summary (cont’d.)
• Dynamic variable: created during execution
– Created using new, deallocated using delete.
• Shallow copy: two or more pointers of the
same type point to the same memory.
• Deep copy: two or more pointers of the same
type have their own copies of the data.

C++ Programming: From Problem Analysis to Program Design, Seventh Edition 40

You might also like