pointer to object in c++

Posted under C++ Course On By mohammed.mutawe

pointer to object in c++

Pointers are one of the most important characteristics of the C++ language, as they carry within them the most important element of the variables, which is addresses, as they control the memory processes in a wide and unlimited way, and this is what distinguishes C++ from other languages ​​that lack this feature.

 

 

Functions

Pointers perform their role perfectly, as they are able to access the addresses of other variables and control their locations in memory, and the pointers  is defined by this special Syntax int *p = &x.

Well, we do not want to complicate matters. If we look at the variable when defining it, we will find that it is composed of:

  • & Reference Address: A unique address in memory followed by certain symbols that no other variable can replace.
  • Value: It is the value that we see when defining the variable. For example, we can say that we have a variable int x = 10, so this is a variable of type integer and has a value equal to 10, but it has a dedicated address in memory that we can access by including a & before the variable and it is called a Reference.

Well, after we understand the shape of variables in memory and what they consist of, we will illustrate the indicators with a simple practical example.

 

Fetch Addresses

We will make an example to fetch the address of a variable that we previously defined in memory:

  • We define an integer variable like this.
#include<iostream>
using namespace std;




int main() {

    int a = 5;


    return 0;
}

 

  • We print the value of the variable followed by its reserved address in memory, so that the code looks like this.
#include<iostream>
using namespace std;




int main() {

    int a = 5;

    std::cout << "This is value of a : " << a << endl;
    std::cout << "This is address of a : " << &a << endl;


    return 0;
}

 

  • Preview and run this code to show the console result like this:

 

Well, now you get the point, please read the lesson again if you find it difficult to distinguish between headings and values.

 

Affecting variables

As we mentioned earlier, one of the most important characteristics and functions of indicators is that they can influence the values ​​of other variables, and now we will define the first indicator and link it to the address of variable a that we defined earlier.

  • We define a pointer and let’s call it *pointer and it is also of type integer, and it is necessary to define the pointers of the type of the variable.

 

#include<iostream>
using namespace std;




int main() {

    int a = 5;

    std::cout << "This is value of a : " << a << endl;
    std::cout << "This is address of a : " << &a << endl;

    int *pointer = &a;


    return 0;
}

 

We note here that the pointer must take the address of the variable during the definition, it is not acceptable in the language to define the pointer and then link it later in the variable address in the main function.

 

  • We increment the value of the variable a by the pointer using the prefix method, so that the shape of the code becomes as follows.
#include<iostream>
using namespace std;




int main() {

    int a = 5;

    std::cout << "This is value of a : " << a << endl;
    std::cout << "This is address of a : " << &a << endl;

    int *pointer = &a;

    ++*pointer;  //This is pointer and we increased a value through it using prefix method


    return 0;
}

 

 

  • In the last step, we will print the value of the variable a to see the effect of the pointer.
#include<iostream>
using namespace std;


int main() {

    int a = 5;

    std::cout << "This is value of a : " << a << endl;
    std::cout << "This is address of a : " << &a << endl;

    int *pointer = &a;

    ++*pointer;

    std::cout <<"This value of a after affected by pointer : "<< a<< endl;


    return 0;
}

 

Example

Please, to understand the indicators well, copy the following codes into your code editor.

#include<iostream>
using namespace std;


int main() {

    int a = 5;

    std::cout << "This is value of a : " << a << endl;
    std::cout << "This is address of a : " << &a << endl;

    int *pointer = &a;

    ++*pointer;

    std::cout <<"This value of a after affected by pointer : "<< a<< endl;


    return 0;
}

 

 

Leave a comment

Your email address will not be published. Required fields are marked *