0% found this document useful (0 votes)
21 views

Chapter 4 - Part 2 - Pointers

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views

Chapter 4 - Part 2 - Pointers

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 32

Chapter Four

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.

 used with arrays, structures and functions.


 we can return multiple values from function using
pointer.

 It makes you able to access any memory location in the


computer's memory.

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 …

 Reference Operator (&)


• Called as address operators
• Used to display the address of a variable
• Refer to the specific location in memory (its memory
address).
• The address that locates a variable within memory is
what we call a reference to that variable.
• Called an ampersand sign (&) symbol
int num=$data;
• Also it is known as reference operator

10
Cont’d …

• The address of variable to pointer we use ampersand


symbol (&).

• They have data type just like variables

• Pointers are used to store addresses rather than values.

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 …

 Difference between reference and dereference operators:


• & is the reference operator and can be read as
"address of"
• * is the dereference operator and can be read as "value
pointed by"
 they have opposite meanings.
 A variable referenced with & can be dereferenced with *.
 Right after these two statements, all of the following
expressions would give true as result:
aa == 25
• •1 aa = 25;
&aa == 1xcyz
•2 ted = &aa ted == 1xcyz
*ted == aa
*ted == 25
16
Pointers and Arrays

 Refer to the address of the first element of the array in variable.


Example:
 supposing these two declarations:

• an array can be considered a constant pointer.


• Therefore, the following allocation would not be valid.
17
Cont’d …

 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 above.
int *ptr;
int arr[5];
ptr = &arr[0];
 The addresses for the rest of the array elements are given
by &arr[1], &arr[2], &arr[3], and &arr[4].

• while assigning the address of array to pointer don’t use


ampersand sign(&)
18
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;

ptr + 1 is equivalent to &arr[1];


ptr + 2 is equivalent to &arr[2];
ptr + 3 is equivalent to &arr[3];
ptr + 4 is equivalent to &arr[4];
21
Cont’d …
• we can access the elements using the single pointer.
• Example:-// use dereference operator
*ptr == arr[0];
*(ptr + 1) is equivalent to arr[1];
*(ptr + 2) is equivalent to arr[2];
*(ptr + 3) is equivalent to arr[3];
*(ptr + 4) is equivalent to arr[4];
• Suppose if we have initialized
ptr = &arr[2]; then
ptr - 2 is equivalent to &arr[0];
ptr - 1 is equivalent to &arr[1];
ptr + 1 is equivalent to &arr[3];
ptr + 2 is equivalent to &arr[4];
22
Cont’d …

23
Array of Pointers

• you can define


arrays to hold a
number of
pointers.

24
Cont’d …

 Array name used


as pointer
 Example:
• Write a C++
Program to
insert and
display data
entered by using
pointer notation.

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 …

 In this example variable


Example
c, which can be used in
three different levels of
indirection, each one of
them would correspond to
a different value:
• c has type char** and
a value of 8092
• *c has type char* and
a value of 7230
• **c has type char and
a value of 'z'

28
Void Pointers

 The void type of pointer is a special type of pointer.


 void represents the absence of type, so void pointers
are pointers that point to a value that has no type
(and thus also an undetermined length and
undetermined dereference properties).

 This allows void pointers to point to any data type,


from an integer value or a float to a string of
characters.

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

• Do not confuse null pointers with void pointers.


• A null pointer is a value that any pointer may take to
represent that it is pointing to "nowhere", while a void
pointer is a special type of pointer that can point to
somewhere without a specific type.

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

You might also like