CHAPTER 2 POINTER
CHAPTER 2 POINTER
POINTERS
12/11/2024 1
Chapters outline
Address (dereference) operator (&)
Pointer Variables
Reference (Indirection ) operator (*)
Pointer Initialization
Pointer arithmetic
The Relation b/n pointers & array
Strings
12/11/2024 2
Address (dereference) operator (&)
The address operator (&) returns the memory address of
a variable.
Suppose the following variable declaration:
float amount;
When the address operator (&) is placed in front of a
variable name, it returns the address of that variable.
Here is an expression that returns the address of the
variable amount:
&amount
Here is a statement that displays the variable’s
address on the screen:
cout << &amount;
12/11/2024 3
… Address (dereference) operator (&)
For example the statement:
ted = &andy;
would assign to variable ted the address of variable andy,
When preceding the name of the variable andy with the
ampersand (&) character we are talking about its
address in memory not its content.
We are going to suppose that andy has been placed in
the memory address 1776 and that we write the
following:
andy = 25;
fred = andy;
ted = &andy;
12/11/2024 4
… Address (dereference) operator (&)
andy = 25;
fred = andy;
ted = &andy;
We have assigned:
to fred the content of variable andy, but
to ted the address in memory where the value of andy is stored
The reason is that in the allocation of ted we have
preceded andy with an ampersand (&) character.
The variable that stores the address of another variable
(like ted in the previous example) is what we call a
pointer.
12/11/2024 5
Pointer Variables
Pointer variables are often called pointers
They are designed to hold memory addresses.
Pointers are special variables that C++ provides
for working with memory addresses.
Just like int variables are designed to hold and
work with integers, pointer variables are
designed to hold and work with addresses.
12/11/2024 6
… Pointer Variables
The definition of a pointer variable looks like the
following:
int *ptr;
The asterisk indicates that ptr is a pointer variable.
The int data type indicates that ptr can be used to
hold the address of an integer variable.
The declaration statement above would read “ptr
is a pointer to an int.”
Note: In this definition, the word int does not
mean that ptr is an integer variable.
Remember, pointers only hold one thing:
addresses.
12/11/2024 7
Declaring Pointer Variables
12/11/2024 9
Reference (Indirection ) operator (*)
With pointer variables you can indirectly manipulate data stored in
other variables.
Using a pointer we can directly access the value stored in the
variable pointed by it.
This is done by preceding the pointer identifier with the reference
operator asterisk (*)
Therefore, if we write:
andy = 25;
fred = andy;
ted = &andy;
beth = *ted;
beth would take
the value 25,
since ted is 1776,
and the value pointed
by 1776 is 25.
12/11/2024 10
… Reference (Indirection ) operator (*)
andy = 25;
ted = &andy;
beth = *ted;
You must clearly differentiate that:
ted stores 1776,
*ted refers to the value stored in the address 1776, that is 25.
Notice the difference of including or not including the reference
asterisk:
beth = ted; // beth equal to ted ( 1776 )
beth = *ted; // beth equal to value pointed by ted ( 25 )
Consider the following statements:
andy = 25;
ted = &andy;
all the following expressions are true:
andy == 25 &andy == 1776
ted == 1776 *ted == 25
12/11/2024
*ted == andy 11
… Reference (Indirection ) operator (*)
Using the expression *ptr is indirectly using the
variable x.
int x = 25;
int *ptr;
ptr = &x;
The following cout statement displays the value
in x twice:
cout << x << " " << *ptr << endl;
And the following statement stores 100 in x:
*ptr = 100;
12/11/2024 12
… Reference (Indirection ) operator (*)
The following program segment demonstrates the use of indirection
operator.
int x = 25;
int *ptr;
ptr = &x; // Store the address of x in ptr
cout << "Here is the value in x, printed twice:";
cout << x << " " << *ptr << endl;
*ptr = 100;
cout << “\nOnce again, here is the value in x:";
cout << x << " " << *ptr << endl;
The above program segment displays the following:
Here is the value in x, printed twice: 25 25
Once again, here is the value in x: 100 100
12/11/2024 13
… Reference (Indirection ) operator (*)
The following program demonstrates the ability of a pointer to point
to different variables.
int x = 25, y = 50, z = 75;
int *ptr;
cout << "Here are the values of x, y, and z:\n";
cout << x << " " << y << " " << z << endl;
ptr = &x; // Store the address of x in ptr
*ptr *= 2; // Multiply value in x by 2
ptr = &y; // Store the address of y in ptr
*ptr *= 2; // Multiply value in y by 2
ptr = &z; // Store the address of z in ptr
*ptr *= 2; // Multiply value in z by 2
cout << "Once again, here are the values of x, y, and z:\n";
cout << x << " " << y << " " << z << endl;
The above program segment displays the following:
Here are the values of x, y, and z: 25 50 75
Once again, here are the values of x, y, and z: 50 100 150
12/11/2024 14
Summary on the two pointer operators
Note: So far you’ve seen three different uses of the
asterisk in C++:
As the multiplication operator, in statements such as
distance = speed * time;
In the definition of a pointer variable, such as
int *ptr;
As the indirection operator, in statements such as
*ptr = 100;
Address or dereference operator(&)
It is used as a variable prefix and
can be translated as "address of",
thus: &variable1 can be read as "address of variable1"
Reference operator (*)
It indicates that what has to be evaluated is the content pointed
by the expression considered as an address.
It can be translated by "value pointed by".
* mypointer can be read as "value pointed by mypointer".
12/11/2024 15
Pointer Initialization
We can explicitly specify to which variable we want
pointers to point during their declaration,
int number;
int *tommy = &number;
this is equivalent to:
int number;
int *tommy;
tommy = &number;
When a pointer assignment takes place we are always
assigning the address where it points to, never the value
pointed.
At the moment of declaring a pointer, the asterisk (*)
indicates only that it is a pointer, not the reference
operator (*).
12/11/2024 16
Pointers and Arrays
Array name can be used as a pointer constant,&
pointer can be used as array name.
The name of an array points only to the first
element not the
1000whole array.
1004
1008
1012
1016
12/11/2024 17
Array Name is a pointer constant
#include <iostream>
using namespace std;
Result:
Address of a[0]: 0x0065FDE4
Name as pointer: 0x0065FDE4
12/11/2024 18
Dereferencing An Array Name
This element is
called a[0] or
*a
#include <iostream>
a[0] 2 using namespace std;
a int main(){
a[1] 4 int a[5] = {2,4,6,8,22};
a[2] 6 cout << *a << " "
<< a[0];
a[3] 8 }
a[4] 22
a
12/11/2024 19
Array Names as Pointers
To access an array, any pointer to the first element
can be used instead of the name of the array.
We could replace *p by *a
#include <iostream>
a p using namespace std;
int main(){
a[0] 2 int a[5] = {2,4,6,8,22};
22
a[1] 4 int *p = a;
a[2] 6 cout << a[0] << " "
a[3] 8 << *p;
a[4] 22 }
12/11/2024
a 20
Multiple Array Pointers
Both a and p are pointers to the same array.
#include <iostream>
a[0] using namespace std;
a[0] 22 int main(){
2 p
44 int a[5] = {2,4,6,8,22};
a[1] 4 int *p = &a[1];
a[2] 6 cout << a[0] << " "
<< p[-1];
a[3] 8 p[0]
cout << a[1] << " "
a[4] 22 << p[0];
}
12/11/2024 21
Pointer Arithmetic
Given a pointer p, p+n refers to the element that
is offset from p by n positions.
a 2 p - 1
a + 1 4 p
a + 2 6 p + 1
a + 3 8 p + 2
a + 4 22 p + 3
12/11/2024 22
Dereferencing Array Pointers
a[0] or *(a + 0) 2 a
a[1] or *(a + 1) 4 a + 1
a[2] or *(a + 2) 6 a + 2
a[3] or *(a + 3) 8 a + 3
a[4] or *(a + 4) 22 a + 4
12/11/2024 25
Home work
6 mark
1. Write a statement that displays the
address of the variable count.
2. Write a statement defining a variable Ptr.
The variable should be a pointer to a
double.
3. List three uses of the * symbol in C++.
12/11/2024 26
4. What is the output of the following program?
#include <iostream>
using namespace std;
int main()
{
int x = 50, y = 60, z = 70;
int *ptr;
cout << x << " " << y << " " << z << endl;
ptr = &x;
*ptr *= 10;
ptr = &y;
*ptr *= 5;
ptr = &z;
*ptr *= 2;
cout << x << " " << y << " " << z << endl;
}
12/11/2024
27
5. Rewrite the following loop so it uses pointer notation
(with the indirection operator) instead of subscript notation.
for (int x = 0; x < 100; x++)
cout << array[x] << endl;
6. Assume ptr is a pointer to an int and holds the address
12000. On a system with 4-byte integers, what address will
be in ptr after the following statement?
ptr += 10;
12/11/2024 28
END OF CHAPTER 2
******** THANK YOU********
***** GOOD LUCK *****
12/11/2024 29