Chapter 3 - Pointers and Pointer Based Strings 3.0
Chapter 3 - Pointers and Pointer Based Strings 3.0
based strings
Pointers
▪ Pointers are special variables in C++ that store the memory address of a
variable.
▪ A pointer can "point" to any variable or data type and can be used to
access the contents of the variable.
▪ Pointers are different from references because a reference variable is
an alias for an existing variable. A pointer can point to any memory
location and can be changed.
▪ Pointers are used to store the address of another variable, but
references are not capable of that. Pointers are also used for dynamic
memory allocation, which means that memory can be allocated as and
when it's needed, making them very useful.
2
Address in C++
3
Pointer Declaration and Initialization
4
Dereferencing pointers
▪ In the example from the previous page, we used the pointer variable to get the
memory address of a variable (used together with the & reference operator).
However, you can also use the pointer to get the value of the variable, by using
the * operator (the dereference operator):
▪ Note that the * sign can be confusing here, as it does two different things in our
code:
– When used in declaration (string* ptr), it creates a pointer variable.
– When not used in declaration, it act as a dereference operator.
5
C++ Modify Pointers
6
Example Working
with pointers
7
Example
Modifying Pointer
values
8
Activity One
9
C++ Pointers and Arrays
▪ Here, ptr is a pointer variable while arr is an int array. The code ptr =
arr; stores the address of the first element of the array in variable ptr.
10
▪ Notice that we have used arr instead of &arr[0]. This is because both
are the same. So, the code below is the same as the code in the
previous slide.
▪ The addresses for the rest of the array elements are given by &arr[1],
&arr[2], &arr[3], and &arr[4].
11
Point to Every Array Elements
12
Note:
13
Example:
However, we should
remember that pointers
and arrays are not the
same.
to pointers.
14
Example 2:
Array name
used as pointer
15
Example 2:
Explained
16
Activities
17
Pointers and Functions
18
Example Explained
19
Example 1
Passing By
Reference
without
Pointers.
20
Example 1 explained
▪ In this program, we passed the variables a and b to the swap() function. Notice
the function definition,
▪ Here, we are using & to denote that the function will accept addresses as its
parameters.
▪ Hence, the compiler can identify that instead of actual values, the reference of
the variables is passed to function parameters.
▪ In the swap() function, the function parameters n1 and n2 are pointing to the
same value as the variables a and b respectively. Hence the swapping takes
place on actual value.
▪ The same task can be done using the pointers.
21
Example 2: Passing by reference using
pointers
22
Example 2 Explained.
▪ Here, the address of the variable is passed during the function call
rather than the variable.
▪ Since the address is passed instead of value, a dereference operator
* must be used to access the value stored in that address.
*n1 and *n2 gives the value stored at address n1 and n2
respectively.
24
C++ new Operator
▪ Here, we have dynamically allocated memory for an int variable using the new
operator.
▪ Notice that we have used the pointer pointVar to allocate the memory
dynamically. This is because the new operator returns the address of the
memory location.
25
Syntax
▪ In the case of an array, the new operator returns the address of the
first element of the array.\
▪ From the example above, we can see that the syntax for using the
new operator is
26
delete Operator
27
▪ Consider the code:
▪ Here, we have dynamically allocated memory for an int variable using the pointer
pointVar.
▪ After printing the contents of pointVar, we deallocated the memory using delete.
28
Note:
29
Example 1: C++
Dynamic Memory
Allocation
▪ In this program, we
dynamically allocated
memory to two variables of
int and float types. After
assigning values to them
and printing them, we
finally deallocate the
memories using the code:
30
Note:
31
Example 2: C++ new and
delete Operator for
Arrays
32
Example 2 explained.