This tutorial aims to explain the concept of pointers and how they can be used to manipulate addresses in memory using C++.
By the end of this tutorial, you will understand what pointers are, how to declare and use them, and how they can be used to manipulate addresses in memory.
Basic knowledge of C++ programming language is required. Familiarity with data types, variables, and functions would be beneficial.
A pointer is a variable that stores the memory address of another variable. The type of the pointer has to match the type of the variable whose address it stores.
To declare a pointer, we use the asterisk *
before the variable name. For example, to declare a pointer to an integer, we would write int *p;
.
The address of operator &
is used to get the memory address of a variable. For example, &num
would give the memory address where the variable num
is stored.
The dereferencing operator *
is used to get the value stored at the memory address that the pointer points to. For example, if p
is a pointer to num
, then *p
would give the value of num
.
Pointers can be used to manipulate memory addresses. By using pointer arithmetic (incrementing or decrementing a pointer), we can access and manipulate data stored in contiguous memory locations, such as arrays.
int num = 10; // Declare an integer
int *p = # // Declare a pointer to the integer
cout << "Address of num: " << &num << endl; // Print the address of num
cout << "Value of p: " << p << endl; // Print the value of p (which is the address of num)
cout << "Value of num: " << num << endl; // Print the value of num
cout << "Value at address stored in p: " << *p << endl; // Print the value at address stored in p (which is the value of num)
Expected Output
The exact addresses will vary, but the output will be similar to:
Address of num: 0x7fff5fbff710
Value of p: 0x7fff5fbff710
Value of num: 10
Value at address stored in p: 10
int arr[3] = {1, 2, 3}; // Declare an array
int *p = arr; // Declare a pointer to the array
for (int i = 0; i < 3; i++) {
cout << *(p + i) << endl; // Print the value at address (arr + i)
}
Expected Output
1
2
3
In this tutorial, we learned about pointers and how to use them to manipulate memory addresses. We discussed how to declare pointers, use the address of and dereferencing operators, and perform pointer arithmetic to manipulate addresses.
The next steps would be to learn about more advanced topics, such as dynamic memory allocation using pointers, and to practice your skills with more complex examples and exercises.
Declare a pointer to a float and use it to change the value of the float.
Declare an array of integers and use a pointer to print the elements of the array.
Create a function that takes a pointer to an integer as a parameter and changes the value of the integer.
Solution to Exercise 1
float num = 5.0;
float *p = #
*p = 6.0; // Change the value of num
cout << num << endl; // Should print 6.0
Solution to Exercise 2
int arr[3] = {1, 2, 3};
int *p = arr;
for (int i = 0; i < 3; i++) {
cout << *(p + i) << endl;
}
Solution to Exercise 3
void changeValue(int *p) {
*p = 10;
}
int main() {
int num = 5;
changeValue(&num);
cout << num << endl; // Should print 10
}