0% found this document useful (0 votes)
6 views33 pages

SOF 103-Chapter 07 - Pointers202409

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)
6 views33 pages

SOF 103-Chapter 07 - Pointers202409

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

1

SOF 103
C and C++ Programming
Chapter 6

Pointers

11/12/24
Pointers
2

A pointer is the memory address of the


variable.

It gives you more control of the


computer’s memory.

Pointers can be used to implement dynamic


arrays and other data structures such as
linked list, stack and queues.
11/12/24
Pointer Variables
3
A pointer can be stored in a variable.

Though pointer is a memory address and a memory


address is an integer, you can’t store a pointer in a
variable of type int or double without typecasting.

A variable to hold a pointer must be declared to have a


pointer type, e.g.,
double *p
The variable p can hold pointers to variables of type
double only.

11/12/24
Cont…
4

Declaration of pointer variables of type int along with


two ordinary variables of type int:
int *p1, *p2, v1, v2;
There must be an asterisk before each of the pointer
variables.
Setting the variable p1 equal to a pointer that points to
the variable v1:
p1 = &v1;
Here, the pointer variable is said to point to the variable
v1 or to be a pointer to the variable v1

11/12/24
Cont…
5

Some pointer declarations:


char *cptr; // pointer to char
int *iptr; // pointer to int
float *fptr; // pointer to float
Distance *distptr; // pointer to user-defined Distance class

int* iptr; or int *iptr;

int *iput, v1;


iput=&v1;

*iput=10; //Here ‘*’ is used as a dereference


operator

11/12/24
A code snippet
6

int *p1, v1;


v1 = 0;
p1 = &v1;
*p1 = 42;
cout << v1 << endl;
cout << *p1 << endl;

This code outputs the following to the screen:


42
42

11/12/24
Uses of Assignment Operator
7

11/12/24
Pointer Variables - Program
8

11/12/24
Accessing the Variable Pointed to
9

11/12/24
Pointers and Dynamic Variables
10

11/12/24
Cont…
11

Program Output

11/12/24
Cont…
12

Explanation

11/12/24
Basic Memory Management
13
freestore – A special area of memory reserved for
dynamic variables.
The size of freestore depends on the system and the
compiler.
If a program creates too many dynamic variables, it
may consume whole freestore area.
Additional calls to new may fail.
It is a good practice to recycle any freestore
memory that is no longer needed.
This can be accomplished by using delete operator:
 delete p;

11/12/24
Defining Pointer Types
14

You can simplify the task of defining pointer


types.

Following defines a type called IntPtr, for


pointer to int variables:
 typedef int* IntPtr;

Thus, the following statements are equivalent:


 IntPtr p;

 int *p;

11/12/24
Dynamic Arrays
15

Array variables are actually pointer variables.


Unlike static array, the size of dynamic array
is specified at run-time.
Given the following two variable declarations,
p and a are the same kind of variable:
int a[10];
typedef int *IntPtr;
IntPtr p;
The value of a can be assigned to the pointer
variable ‘p’ as follows:
p=a;
11/12/24
Array variable is a kind of pointer
variable
16
#include<iostream>
using namespace std;

int main()
{
typedef int *IntPtr;
IntPtr p;
int a[10];
int i;
for(i=0;i<10;i++)
a[i]=i;
p=a;
for(i=0;i<10;i++)
cout<<p[i]<<" ";
cout<<endl;

for(i=0;i<10;i++)
p[i]=p[i]+1;

for(i=0;i<10;i++)
cout<<a[i]<<" ";
cout<<endl;
return 0;
}
11/12/24
Explanation
17

11/12/24
Explanation
18

11/12/24
How to Use a Dynamic Array?
19

Define a pointer type:


 typedef double* DoubleArrayPtr;

Declare a pointer variable:


 DoubleArrayPtr a;
Size can be an int
Call new: variable or an
expression, and is
 a = new double[array_size];
determined at run-time
Use like an ordinary array:
 a[0], a[1] and so on…

Call delete[]:
 delete [] a;

11/12/24
The Relationship between Pointers and
Arrays
20

Consider the following declarations:


 int b[5];
 int *bptr;

Equivalent

 bptr = b;
 bptr = &b[0];

11/12/24
Cont…
21

 Array element b[3] can alternatively be referenced with the


pointer expression,
*(bPtr + 3)
 The 3 in the above expression is the offset to the pointer. The
parentheses () are necessary because the precedence of * is
higher than the precedence of +. Without the parentheses (),
the above expression would add 3 to the expression *bPtr
(i.e. 3 would be added to the b[0] assuming bPtr points to the
beginning of the array).

 The address &b[3] can be written with the pointer expression


bPtr + 3

11/12/24
Cont…
22

 The array itself can be treated as a pointer and used in


pointer arithmetic. For example, the expression
*(b + 3)
also refers to the array element b[3].

 Pointers can be subscripted exactly as arrays can. For


example, the expression,
bPtr[1]
refers to the array element b[1]. This expression is
referred to as pointer/subscript notation.

11/12/24
Cont…
23

The array name is a constant pointer. It


always points to the beginning of the
array. Therefore, the expression
b += 3
is invalid because it attempts to modify
the value of the array name with pointer
arithmetic.

11/12/24
Program to demonstrate the
relationship between array and
pointer
24

11/12/24
Cont…
25

11/12/24
Output
26

11/12/24
Pointer to a string and character
array
27

 A string may be assigned in a declaration to either a


character array or a variable of type char *.
 Consider the declarations:
char colour[] = “blue”;
char *colourPtr = “blue”;
 Each initialize a variable to the string “blue”.

 The first declaration creates a 5-element array colour


containing characters ‘b’, ‘l’, ‘u’, ‘e’ and ‘\0’.

 The second declaration creates a pointer variable colourPtr


that points to the string “blue” somewhere in memory.

11/12/24
Array of Pointers
28

 Array may contain pointers. A common use of this


data structure is to form an array of strings,
referred to as string array.
 Each entry in the array is a string, but in C++ a
string is essentially a pointer to its first character.
 So each entry in an array of strings is actually a
pointer to the first character of a string.
 An example is
char *colour[3] = {“red”, “green”, “blue”};
 The colour[3] indicates an array of 3 elements.
The char * indicates that each element of array
colour is of type “pointer to char”.

11/12/24
Cont…
29

Each of these is stored in memory as a null-


terminated character string that is one
character longer than the number of
characters between quotes.
The three strings are 4, 6 and 5 characters
long, respectively.
A graphical representation of an array of
pointer is shown in example below.
colour[0]  ‘r’ ‘e’ ‘d’ ‘\0’
colour[1]  ‘g’ ‘r’ ‘e’ ‘e’ ‘n’ ‘\0’
colour[2]  ‘b’ ‘l’ ‘u’ ‘e’ ‘\0’
11/12/24
Program to Demonstrate Array of
Pointers
30

11/12/24
Output
31

11/12/24
References
32

1. Problem Solving with C++ (10th Edition), Walter Savitch,


Addison Wesley, 2018
2. Paul J. Dietel, C++ How to Program (10th Edition), Pearson,
2017.
3. Turbo C programming for the PC, Robert Lafore
4. Dr. Kamran’s notes.

11/12/24
Notes
33

1. When should we use pointers?


 Call by reference / pointer.
 Dynamic memory allocation and dynamic arrays.
 C strings.
 Dynamic data structures.

2. If not needed, avoid using pointers.


 In the past, using pointers may enhance the run-time efficiency
at the implementation level.
 Nowadays, compilers are good at implementation level efficiency
optimization.
 Readability is more important.

11/12/24

You might also like