0% found this document useful (0 votes)
9 views35 pages

Lecture2 Csc0015 Module2 Week2

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)
9 views35 pages

Lecture2 Csc0015 Module2 Week2

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/ 35

MODULE 2

Review of Pointers
Module 4 Topic in
Computer Programming 2
• Learn about the pointer data type and pointer
variables
• Explore how to declare and manipulate pointer
variables
• Learn about the address of operator and the
dereferencing operator
• Learn the application of pointers to data structures
• Implement pointers and data structures in programs.
• Pointer • Dynamic Arrays
• Address Operator • Arrays and Pointers
• Dereferencing Operator • Structures and Pointers
• The new Operator • Classes and Pointers
• The heap
• The delete Operator
• Pointer → is the memory address of a variable
• Pointer variable → content is a memory address
• There is no name associated with the pointer data
type in C++
• Discover dynamic variables
• Explore how to use the new and delete operators
to manipulate dynamic variables
• Syntax:

• Examples:
int *p;
char *ch;
• These statements are equivalent:
int *p;
int* p;
int * p;
• In the statement: int* p, q; only p is the
pointer variable, not q; here q is an int
variable
• To avoid confusion, attach the character * to
the variable name:
int *p, q;
int *p, *q;
• The ampersand, &, is called the address of
operator
• The address of operator is a unary operator that
returns the address of its operand
• The & in front of an ordinary variable produces the
address of that variable; that is, it produces a
pointer that points to the variable. The & operator
is simply called the address-of-operator.
• When used as a unary operator, * is the
dereferencing operator or indirection operator
• Refers to object to which its operand points
• The * operator in front of a pointer variable
produces the variable to which it points. When
used this way, the * operator is called the
dereferencing operator
• Example:

• To print the value of x, using p:

• To store a value in x, using p:


Before: After:
*p1 = 10; p1 = p2;
*p2 = 20;
Before: After:
*p1 = 10; *p1 = *p2;
*p2 = 20;
Sample Program 1
Before: After:
int num = 100; *pointer = 250;
pointer = #
Sample Program 2
Sample Program 2 Illustration
• Since a pointer can be used to refer to a variable, your
program can manipulate variables even if the variables
have no identifiers to name them.
• The new operator creates a new dynamic variable of a
specified type and returns a pointer that points to this new
variable.
• Example:
p1 = new int;
This new, nameless variable can be referred to as *p1 (that
is, as the variable pointed by p1)
Sample Program 3
Sample Program 3 Illustration
• It is a special area of memory that is reserved for
dynamically allocated variables.
• Any new dynamic variable created by a program
consumes some of the memory in the freestore.
• If there was insufficient available memory to create
the new variable, then new returned a special value
named NULL.
• Eliminates a dynamic variable and returns the memory that
the dynamic variable occupied to the freestore manager so
that the memory can be reused.
• When you apply delete to a pointer variable, the dynamic
variable to which it is pointing is destroyed.
• At that point, the value of the pointer variable is undefined,
which means that you do not know where it is pointing.
• If pointer variable pointing to the dynamic variable that was
destroyed and becomes undefined is called dangling
pointers.
Sample Program 4
• You can assign a name definition and then use the
type name to declare variables using typedef
keyword.
• Syntax:
Sample Program 5
Pointers as Call-by-Value

Sample Program 6
Sample Program 6 Illustration
• Is an array whose size is not specified when you write
the program, but is determined while the program is
running.

• Legal:

• Illegal:
Sample Program 7
• Illustration: d contains the address of the indexed
variable d[0]. The expression d + 1 evaluates to the
address of d[1], d + 2 is the address of d[2], , and so
forth.
• Is equivalent to
Sample Program 8
Sample Program 9
Classes & Pointers

Sample Program 10
Classes & Pointers

Sample Program 10
continuation
• Bancila, M.(2017). Modern C++ Programming Cookbook: Recipes to explore
data structure, multithreading, and networking in C++17. Birmingham, UK:
Packt Publishing
• Dr Kung-Hua Chang(2017). Data Structures Practice Problems for C++
Beginners. USA: Simple & Example
• Wittenberg, Lee(2017). Data Structures and Algorithms in C++: Pocket Primer.
New Delhi: Mercury Learning & Information
• Hemant Jain(2017). Problem Solving in Data Structures & Algorithms Using
C++: Programming Interview Guide. USA: CreateSpace Independent
Publishing Platform
• Albert, J.(2016). Programming with Visual C++: Concepts and Projects. USA:
Cengage Learning

You might also like