Chapter 4 - Part 2 - Pointers
Chapter 4 - Part 2 - Pointers
Pointers
Part 2
By: Sinodos G.
Pointers
Pointers are used to store addresses rather than values.
The pointer is a variable, it is also known as locator or
indicator that points to an address of a value.
Pointers are variables that store the memory addresses of
other variables.
a variable that holds the address of another variable.
2
C++ pointers
Like regular variables, pointers have a data type.
Example:
• a pointer of type integer can hold the address of a variable of type
integer.
• A pointer of character type can hold the address of a variable of
character type.
Some C++ tasks are performed more easily with pointers
• dynamic memory allocation, cannot be performed without them.
Memory location:-
every variable is a memory location
every memory location has its address
Defined and accessed using ampersand (&) operator which denotes
an address in memory.
3
Creating Pointers
need to declare a pointer before you can use it. However, pointer
names are preceded by an asterisk(*)
Syntax:
data_type *pointer_name;
Example:
int* myPointer, myVar; //a pointer and an int
int *ip; // pointer to an integer
double *dp; // pointer to a double
float *fp; // pointer to a float
char *ch // pointer to character
The above lines are all valid ways to declare a pointer.
4
Cont’d …
5
Using Pointers in C++
There are few important operations, which we will do with
the pointers very frequently.
• define a pointer variable.
• assign address of a variable to a pointer.
• Finally access the value at the address available in the
pointer variable.
This is done by using unary operator * that returns the
value of the variable located at the address specified by its
operand.
6
Cont’d …
Advantage of pointer
Pointer reduces the code and improves the performance,
it is used to retrieving strings, trees etc.
7
Cont’d …
Example
8
Pointer Operators
Dereference operator (*)
• Represented as variable and * operator
• Used to dereference of a variable
• There are three ways to declare pointer variables,
string* data; // Preferred
string *data;
string * data;
9
Cont’d …
10
Cont’d …
11
Changing Value Pointed by Pointers
If pointVar points to the address of var, we can change the
value of var by using *pointVar.
For example:-
12
Cont’d …
Example:
string food = "Pizza";
string* ptr = &food;
// Output the value of food (Pizza)
cout << food << "\n";
// Output the memory address of food (0x6dfed4)
cout << &food << "\n";
// Access the memory address of food and output its value (Pizza)
cout << *ptr << "\n";
// Change the value of the pointer
*ptr = "Hamburger";
// Output the new value of the pointer (Hamburger)
cout << *ptr << "\n";
// Output the new value of the food variable (Hamburger)
cout << food << "\n";
13
Cont’d ..
Exercise:
Create a pointer
string food = "Pizza";
// A food variable of type string
variable with the string* ptr = &food;
name ptr, that should // A pointer variable, with the name ptr, that stores the
point to a string address of food
// Output the value of food (Pizza)
variable named food: cout << food << "\n";
// Output the memory address of food (0x6dfed4)
cout << &food << "\n";
String food = // Output the memory address of food with the pointer
(0x6dfed4)
"Pizza";
cout << ptr << "\n";
string *ptr =
&food;
14
Example
15
Cont’d …
19
Example:
20
Point to Every Array Elements
• Suppose we need to point to the fourth element of the
array using the same pointer ptr.
• Here, if ptr points to the first element in the above
example then ptr + 3 will point to the fourth element.
int *ptr;
int arr[5];
ptr = arr;
23
Array of Pointers
24
Cont’d …
25
Access Array Elements Using Pointer
26
Pointers to Pointers
a form of multiple indirection or a chain of pointers.
In order to do that, we only need to add an asterisk (*)
for each level of reference in their declarations:
1 char a; • This, supposing the randomly chosen
2 char * b; memory locations for each variable of
3 char ** c; 7230, 8092 and 10502, could be
4 a = 'z'; represented as:
5 b = &a;
6 c = &b;
int **var;
27
Cont’d …
28
Void Pointers
29
Null Pointer
A null pointer is a regular pointer of any pointer type
which has a special value that indicates that it is not
pointing to any valid reference or memory address.
This value is the result of type-casting the integer value
zero to any pointer type.
1 int * p;
2 p = 0; // p has a null pointer value
30
• A pointer that is assigned NULL is called a null pointer.
• NULL pointer is a constant with a value of zero defined in
several standard libraries, including iostream.
31
Question
32