Indicators in C Plus Plus C++
In the previous lesson, we learned about references ( References ) And we learned how they are used with the goal of making us able to directly access things defined in memory.
Indications ( Pointers ) It is also used to access anything that is defined in memory and it gives us more control advantages.
The difference between a reference and a pointer in C++
The main difference between them is that a pointer reserves space in memory to store the address of the thing it is pointing at.
Additionally, a pointer can point to anything in memory at any time and you don't have to specify what it's pointing to at the moment of its creation.
In this lesson, you will understand the difference between references and pointers in relation to memory and learn about the additional advantages that pointers provide.
Define pointer in C++
To define a new pointer we use the symbol *
with the indication that the type of pointer must be the same as the type of thing it will point to in memory.
If we want to define an indicator of its type int
and name, x
we have three options as follows.
// The first method is the most widely used int* x; // second method int *x; // third method int * x;
Now, to define a pointer and make it point to the value of something in memory, we must pass the address of that thing as the pointer's value and then the pointer will be able to access its value and address as we will see in the following example.
An example of defining a pointer in C++
#include <iostream> using namespace std; int main() { // "Arabic" and its value language here we have defined a variable named string language = "Arabic"; // In memory language here we have defined a pointer to the address of the variable string* ptr = &language; // language Here we have printed the value of the variable cout << "language = " << language << endl; // In memory language here we have printed the address of the variable cout << "Address of language = " << &language << endl; // in memory ptr here we have printed the address of the pointer cout << "Address of ptr in memory = " << &ptr << endl; // language which is the address of the ptr variable here we have printed the value in the pointer cout << "Value of ptr in memory = " << ptr << endl; // language in memory which is the value of the ptr variable here we have printed the value pointed to by the pointer address cout << "Value that ptr point to = " << *ptr; return 0; }
• We will get a result similar to the following when running.
language = Arabic Address of language = 0x61fde0 Address of ptr in memory = 0x61fdd8 Value of ptr in memory = 0x61fde0 Value that ptr point to = Arabic
The following image shows how the address and value of the variable language
and pointer are stored ptr
in memory.
It is very important that you notice the way the pointer is created in memory so that you know how to handle it easily.
The pointer
ptr
has been allocated a special space in memory, and this space has an address and a value as well.The pointer value
ptr
is the address of the variablelanugage
in memory, which means that the pointer value is always the address of the thing it points to in memory.
As for dealing with the pointer, we noticed the following:
We write the name of the indicator only if we want to access its value, for example
ptr
We put
&
it before the name of the pointer if we want to access its address in memory, for example&ptr
We put
*
before the name of the pointer in case we want to access the value of the thing it points to, for example*ptr
Determine the thing that is referred to in C++
In the previous example, we noticed that a pointer in memory is very similar to the thing it points to, as it has a value and a special address in memory.
We also noticed that in the pointer we do not put normal values, as its value is the address of the thing it points to in memory and through which we can access its value.
Now if you want to make the pointer point to something else in memory, all you have to do is pass the address of the other thing to it as a value.
In the following example, we have defined two variables named x
. and . y
.
Then we define a pointer whose name ptr
we make it point to the variable x
and display it.
Then we have ptr
it point to the variable y
and show the value it points to.
An example of identifying the thing that is referred to in C++
#include <iostream> using namespace std; int main() { // its value is 2 y its value is 1 and the variable x here we have defined a variable int x = 1; int y = 2; // ptr Here we have defined a pointer named int* ptr; // x points to the address of the ptr variable here we have made the pointer ptr = &x; // x which is the value of the ptr variable here we have printed the value of the variable it points to cout << "*ptr = " << *ptr << endl; // y points to the address of the ptr variable here we made the pointer ptr = &y; // y again which became the value of the ptr variable here we print the value of the variable it points to cout << "*ptr = " << *ptr << endl; return 0; }
• We will get the following result when running.
*ptr = 1 *ptr = 2
The following video shows how the value of the pointer was changed ptr
as it was made to point to the address of the variable x
and then to the address of the variable y
.
Change the value of the thing the pointer is pointing at C++
In the following example, we have defined a variable whose name is x
its value 1
, and its name pointer ptr
refers to the variable x
.
Through the indicator, ptr
we changed the value of the variable x
and then we printed its value to be sure.
Example of changing the value of the thing the pointer points to in C++
#include <iostream> using namespace std; int main() { // its value is 1 x here we have defined a variable named int x = 1; // in memory x and made it point to the address of the variable ptr here we have defined a pointer named int* ptr = &x; // to 5 ptr here we have changed the value of the variable the pointer is pointing to *ptr = 5; // To check whether x has changed or not, here we print the value of the variable cout << "x = " << x << endl; // x which is the same as the value of the ptr variable here we have printed the value of the variable it points to cout << "*ptr = " << *ptr; return 0; }
• We will get the following result when running.
x = 5 *ptr = 5
Examples about advantages of using indicators in C++
How to create an indicator that has no value in C++
If you want to create a pointer that does not have a value, you must give it the value NULL
or the value nullptr
to ensure that it is empty and does not have any default values.
When you pass one of these two values to it, its value becomes equal 0
, and then it categorically does not refer to anything in memory because it can only be the address of something in memory 0
.
A note about using indices that have no value in C++
A value nullptr
is an assigned value in a language C++ to deal with pointers.
The value NULL
can be used with pointers and anything else we want to ensure that has no value.
In the following example, we create a pointer and print the default value in it
The first example is to create a pointer and print the default value in it
#include <iostream> using namespace std; int main() { // without making it point to the value of something in memory x here we define a pointer named int* x; // In memory x here we have printed the address of the thing the pointer is pointing at cout << "x = " << x; return 0; }
• We will get a result similar to the following when running.
• We notice that the indicator x
has a random value.
x = 0x10
Here we put an example on how to create an indicator that has no value ( Null Pointer ) In addition to how to check the indicator to see if it has a value or not.
Example 2: Creating a pointer that doesn't have a value ( Null Pointer )
#include <iostream> using namespace std; int main() { // and make it not point to anything in memory x here we have defined a pointer named int* ptr = NULL; // does not point to something in memory ptr The print command placed here will be executed if the pointer is if (!ptr) { cout << "ptr is null"; } // points to something in memory ptr The print command placed here will be executed if the pointer is else { cout << "ptr is not null"; } return 0; }
• We will get the following result when running.
ptr is null
So when checking the value of the indicator, we write (!ptr)
if we want to execute orders if the indicator is empty.
And we write (ptr)
if we want to execute orders in case the cursor is pointing to something.
Arithmetic operators and indicators in C Plus Plus
When we define an array, we define one thing and specify the number of its elements.
Then the compiler creates all the elements of the array in order behind each other in memory.
If you add a pointer to define the array, you are able to move between its elements using arithmetic operators -
, --
, +
, ++
.
Information
When we use a pointer to move between the elements of an array, we make the pointer equal to the array as it is.
Then the address of the first element of the array will be put as the value of the pointer as you will see in the following example.
In the following example, we have defined an array named fruit
containing three text values.
Then we create a pointer named ptr
and make it point to the array.
Example
#include <iostream> using namespace std; int main() { // Contains three values fruit Here we have defined an array named string fruit[3] = {"Apple", "Banana", "Orange"}; // fruit and we made it point to the elements of the ptr array. Here we have defined a pointer named string* ptr = fruit; // and naturally it will be the first value found in the ptr array here we have printed the value of the element the pointer is pointing to cout << *ptr << endl; // In order to move to the next element in the ptr array, here we add 1 to the address of the element in the pointer ptr++; // Naturally, it will be the second value in the ptr array. Here we have printed the value of the element the pointer is pointing to cout << *ptr << endl; // In order to move to the next element in the ptr array, here we add 1 to the address of the element in the pointer ptr++; // Naturally, it will be the third value in the ptr array. Here we have printed the value of the element the pointer is pointing to cout << *ptr; return 0; }
• We will get a result similar to the following when running.
• We notice that the indicator x
has a random value.
Apple Banana Orange
An example of defining a function that takes a parameter of a pointer and calls it
The following example teaches you how to create a function that takes a parameter that is a pointer and how to pass a value to the parameter when it is called.
Here we define a function named swap
, When called, we pass two variables to it and it replaces their values.
Example
#include <iostream> using namespace std; // Because we will refer to these addresses with b and a when called, we pass them the address of two variables in place of the two parameters swap. Here we have defined a function named void swap(int *a, int *b) { // in memory b and a here the function will tweak the values of the two variables int temp = *a; *a = *b; *b = temp; } int main() { // its value is 5 y and its value is 3 and a variable named x here we have defined a variable named int x = 3; int y = 5; // it has to swap their values of y and x and pass the address of the two variables swap() here we called the function swap(&x, &y); // whether their values have changed or not y and x here we have printed the value of the two variables cout << "x = " << x << endl; cout << "y = " << y; return 0; }
• We will get the following result when running.
x = 5 y = 3