Pointers
Pointers
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 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;
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
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