KEMBAR78
Chapter 8 Pointers: Lecturer: Mrs Rohani Hassan | PDF | Pointer (Computer Programming) | C++
0% found this document useful (0 votes)
183 views20 pages

Chapter 8 Pointers: Lecturer: Mrs Rohani Hassan

This document discusses pointers in C++. It begins by stating the objectives of learning about pointers, which include describing what a pointer is, declaring and assigning values to pointers, accessing elements via pointers, passing arguments by reference with pointers, and understanding the relationship between arrays and pointers. It then provides an introduction to pointers, explaining that they reference the address of arrays, objects, or variables in memory and enable direct manipulation of memory. It proceeds to describe how to declare pointers, assign addresses to pointers, dereference pointers to access the referenced value, ensure pointer and variable types match, initialize pointers, pass pointers as function arguments, understand the relationship between arrays and pointers, and use pointers to access array elements. It stresses the importance of initializing

Uploaded by

SK Kepong
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)
183 views20 pages

Chapter 8 Pointers: Lecturer: Mrs Rohani Hassan

This document discusses pointers in C++. It begins by stating the objectives of learning about pointers, which include describing what a pointer is, declaring and assigning values to pointers, accessing elements via pointers, passing arguments by reference with pointers, and understanding the relationship between arrays and pointers. It then provides an introduction to pointers, explaining that they reference the address of arrays, objects, or variables in memory and enable direct manipulation of memory. It proceeds to describe how to declare pointers, assign addresses to pointers, dereference pointers to access the referenced value, ensure pointer and variable types match, initialize pointers, pass pointers as function arguments, understand the relationship between arrays and pointers, and use pointers to access array elements. It stresses the importance of initializing

Uploaded by

SK Kepong
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/ 20

Chapter 8 Pointers

Lecturer: Mrs Rohani Hassan

Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved.
013225445X
1
Objectives
 To describe what a pointer is (§7.1).
 To learn how to declare a pointer and assign a value to it (§7.2).
 To access elements via pointers (§7.2).
 To pass arguments by reference with pointers (§7.3).
 To understand the relationship between arrays and pointers (§7.4).
 To know how to access array elements using pointers (§7.5).
 To declare constant pointers and constant data (§7.5).
 To learn how to return pointers from functions (§7.6).
 To use the new operator to allocate persistent memory dynamically (§7.7).

Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved.
013225445X
2
Introduction
Pointer is one of the most powerful features in C++. You can use a
pointer to reference the address of an array, an object or any
variable.

Pointer enable you to directly manipulate computer memory and


manage dynamic data structures.

Pointer is the heart and soul of the C++ programming language.


Many of the C++ features and library are built using pointers.

Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved.
013225445X
3
What is a Pointer?
Pointer variables, simply called pointers, are declared to hold
memory addresses as their values.
Normally, a variable contains a data value, e.g., an integer, a
floating-point value, and a character.
However, a pointer contains the memory address of a variable that in
turn contains a data value.

Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved.
013225445X
4
Declare a Pointer
Like any other variables, pointers must be declared before they can
be used. To declare a pointer, use the following syntax:

dataType *pVarName;

Each variable being declared as a pointer must be preceded by an


asterisk (*). For example, the following statement declares a pointer
variable named pCount that can point to an int varaible.
pCount count
int *pCount; Address of Address of variable count
Address of variable count 5
pCount

TestPointer Run
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved.
013225445X
5
Program Name: TestPointer.cpp
#include <iostream>
using namespace std;

int main()
{
int count = 5;
int *pCount = &count;

cout << "The address of count is " << &count << endl;
cout << "The address of count is " << pCount << endl;
cout << "The value of count is " << count << endl;
cout << "The value of count is " << *pCount << endl;
cout << "The address of pCount is " << &pCount << endl;

return 0;
}
Output:
The address of count is 0013FF60
The address of count is 0013FF60
The value of count is 5
The value of count is 5
The address of pCount is 0013FF84

Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved.
013225445X
6
Assign Address
You can now assign the address of a variable to a pointer.
For example, the following code assigns the address of variable
count to pCount
int count = 5;
pCount = &count;

The ampersand (&) symbol is called the address operator when


placed in front of a variable. It is a unary operator that returns the
variable’s address.

Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved.
013225445X
7
Dereferencing
Referencing a value through a pointer is called indirection. The
syntax for referencing a value from a pointer is

*pointer

For example, you can increase count using

count++; // direct reference

or

(*pCount)++; // indirect reference

Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved.
013225445X
8
Pointer Type
A pointer variable is declared with a type such as int or double.
You have to assign the address of the variable of the same type.
It is a syntax error if the type of the variable does not match the
type of the pointer.

For example, the following code is wrong.

int area = 1;
double *pArea = &area; // Wrong

Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved.
013225445X
9
Initializing Pointer
Like a local variable, a local pointer is assigned an arbitrary value if
you don’t initialize it.
A pointer may be initialized to 0, which is a special value for a
pointer to indicate that the pointer points to nothing. You should
always initialize pointers to prevent errors.
Dereferencing a pointer that is not initialized could cause fatal
runtime error or it could accidentally modify important data.

Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved.
013225445X
10
Caution
You can declare two variables on the same line. For example, the
following line declares two int variables:

int i = 0, j = 1;

Can you declare two pointer variables on the same line as follows?

int* pI, pJ;

No, this line is equivalent to

int *pI, pJ;

Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved.
013225445X
11
Passing Arguments by Reference
with Pointers
There are three ways to pass arguments to a
function in C++: pass by value, pass by reference
with reference arguments, and pass by reference
with pointers.

TestPointerArgument Run

Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved.
013225445X
12
Arrays and Pointers
Recall that an array variable without a bracket and a subscript
actually represents the starting address of the array. In this sense, an
array variable is essentially a pointer. Suppose you declare an array
of int value as follows:

int list[6] = {11, 12, 13, 14, 15, 16};

list list+1 list+2 list+3 list+4 list+5

11 12 13 14 15 16

Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved.
013225445X
13
Array Pointer
*(list + 1) is different from *list + 1. The
dereference operator (*) has precedence over +.
So, *list + 1 adds 1 to the value of the first
element in the array, while *(list + 1) dereference
the element at address (list + 1) in the array.

ArrayPointer Run

PointerWithIndex Run

Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved.
013225445X
14
Using const with Pointers
You learned how to declare a constant using the const
keyword. A constant cannot be changed once it is
declared. You can declare a constant pointer. For
example, see the following code:
ConstParameter Run
double radius = 5;
double * const pValue = &radius;

Constant data Constant pointer

const double * const pValue = &radius;

Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved.
013225445X
15
Returning Pointers from Functions
You can use pointers as parameters in a function.
Can you return a pointer from a function? The
answer is yes.
WrongReverse Run

CorrectReverse Run

Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved.
013225445X
16
Program Name: CorrectReverse.cpp
#include <iostream> void printArray(const int *list, int size)
using namespace std; {
for (int i = 0; i < size; i++)
int *reverse (const int *list, int size) cout << list[i] << " ";
{ }
int *result = new int[size];
int main()
for (int i = 0, j = size - 1; {
i < size; i++, j--) int list[] = {1, 2, 3, 4, 5, 6};
{ int *pList = reverse(list, 6);
result[j] = list[i]; printArray(pList, 6);
}
return 0;
return result; }
}
Output:
6 5 4 3 2 1

Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved.
013225445X
17
Storing and Accessing Strings
A pointer-based string in C++ is an array of characters
ending in the null terminator ('\0'), which indicates where a
string terminates in memory. An array can be accessed via a
pointer. So a string can also be accessed via a pointer,
which points to the first character in the string. So you can
declare a string variable using an array or a pointer. For
example, the following two declarations are both fine:

char city[7] = "Dallas"; // Option 1


char *pCity = "Dallas"; // Option 2

Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved.
013225445X
18
Pointer Syntax
You can access city or pCity using the array syntax or
pointer syntax. For example,

cout << city[1] << endl;


cout << *(city + 1) << endl;
cout << pCity[1] << endl;
cout << *(pCity + 1) << endl;

each displays character a (the second element in the


string).

Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved.
013225445X
19
Asterisk (*)
The asterisk (*) can be used in 3 different way in C++:
 As a multiplication operator, such as
double area = radius * radius * 3.14159
 To declare a pointer variable, such as
int *pCount = &count
 As the indirection operator, such as
*pCount

The compiler can tell what the symbol * is used for in a


program

Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved.
013225445X
20

You might also like