0% found this document useful (0 votes)
14 views15 pages

Pointers

Uploaded by

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

Pointers

Uploaded by

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

LECTURE 11

Pointers

1
Contents
 Pointer Variable Declaration and
Initialization
 Pointer Operators
 Calling Function by Reference

2
Introduction
 A pointer is a variable in C++ that stores
the memory address of another variable.
Instead of holding data directly, it holds
the address where the data is stored.
Pointers are a powerful feature of C++
that allow for efficient memory
management, dynamic memory allocation,
and working with arrays and functions.
 Close relationship with arrays and strings
How to use a pointer?
 Define a pointer variable
 Assigning the address of a variable to a
pointer using the unary operator (&) which
returns the address of that variable.
 Accessing the value stored in the address
using unary operator (*) which returns the
value of the variable located at the
address specified by its operand.

4
Pointer Variable Declarations
and Initialization
count
countPtr count
7 7
 Pointer variables
 Pointers contain the address of a variable that has a
specific value (indirect reference)
 Pointer declarations
 * indicates variable is a pointer
int *myPtr;
declares a pointer to an int, a pointer of type int
*
 Multiple pointers require multiple asterisks
int *myPtr1, *myPtr2;
Pointer Operators
 & (address operator)
 Returns the address of its operand
 Example
int y = 5;
int *yPtr;
yPtr = &y; // yPtr gets address of y
 yPtr “points to” y

y yptr y
5 500000 600000 600000 5
yPtr

address of y
is value of
yptr
Pointer Operators
 Returns the value of what its operand points
to
 *yPtr returns y (because yPtr points to y).
 * can be used to assign a value to a location
in memory
*yptr = 7; // changes y to 7
8
1 // Using the & and * operators.
2 #include <iostream>
3 using namespace std;
4
5 int main()
6 {
7 int a; // a is an integer
8 int *aPtr; // aPtr is a pointer to an integer
9
10 a = 7;
11 aPtr = &a; // aPtr assigned address of a
12
13 cout << "The address of a is " << &a
14 << "\nThe value of aPtr is " << aPtr;
15
16 cout << "\n\nThe value of a is " << a
17 << "\nThe value of *aPtr is " << *aPtr;
18 return 0; // indicates successful termination
19 }

The address of a is 0012FED4


The value of aPtr is 0012FED4

The value of a is 7
The value of *aPtr is 7
1. #include <iostream>
2. #include <string>
3. using namespace std;
4. int main() {
5. string city = “Tashkent";
6. // create a reference to the variable
7. string* ref_city = &city;
8. // display the variable
9. cout << "Variable Value = " << city << endl;
10. cout << "Reference Value = " << *ref_city << endl;

11. // modify the variable using reference


12. *ref_city = "New York";

13. // display the variable


14. cout << endl << "After Modification: " << endl;
15. cout << "Variable Value = " << city << endl;
16. cout << "Reference Value = " << *ref_city << endl;
17. return 0;
18. }

Variable Value =Tashkent


Reference Value =Tashkent
After modification:
Variable Value =New York
Reference Value = New York
Calling Functions by
Reference
 3 ways to pass arguments to function
 Pass-by-value
 Pass-by-reference with reference arguments
 Pass-by-reference with pointer arguments
 Arguments passed to function using
reference arguments
 Modify original values of arguments

10
Calling Functions by
Reference
 Call by reference with pointer arguments
 Pass address of argument using & operator
 Allows you to change actual location in memory
 Arrays are not passed with & because the array name
is already a pointer
 * operator used as alias/nickname for variable inside
of function
void doubleNum( int *number )
{
*number = 2 * ( *number );
}
 *number used as nickname for the variable passed in
 When the function is called, must be passed an
address
doubleNum( &myNum );
1
2 // Cube a variable using pass-by-reference
3 // with a pointer argument.
4 #include <iostream>
5
6 using namespace std;
7
8
9 void cubeByReference( int * ); // prototype
10
11 int main()
12 {
13 int number = 5;
14
15 cout << "The original value of number is " << number;
16
17 // pass address of number to cubeByReference
18 cubeByReference( &number );
19
20 cout << "\nThe new value of number is " << number << endl;
21
22 return 0; // indicates successful termination
23
24 } // end main
25
26 // calculate cube of *nPtr; modifies variable number in main
27 void cubeByReference( int *nPtr )
28 {
29 *nPtr = *nPtr * *nPtr * *nPtr; // cube *nPtr
30
31 } // end function cubeByReference

The original value of number is 5


The new value of number is 125
1. #include <iostream>
2. using namespace std;
3. // Function to swap two integers using pointers
4. void swap(int* ptr1, int* ptr2) {
5. // Dereference the pointers to swap the values
6. int temp = *ptr1;
7. *ptr1 = *ptr2;
8. *ptr2 = temp;
9. }
10. int main() {
11. int a = 10;
12. int b = 20;

13. cout << "Before swapping:" << endl;


14. cout << "a = " << a << ", b = " << b << endl;

15. // Call the swap function and pass the addresses of a and b
16. swap(&a, &b);
17. cout << "\nAfter swapping:" << endl; OUTPUT
18. cout << "a = " << a << ", b = " << b << endl;
19. return 0;
20. }

14
 Pointers are essential for efficient memory
management and flexible data
manipulation. They allow direct access to
memory, dynamic memory allocation, and
passing large amounts of data efficiently.
Pointers should be used carefully to avoid
memory leaks, segmentation faults, and
other issues related to memory access.

15

You might also like