Pointers: How to declare, Initialize, and Use

Now that we've learned what pointers and memory addresses are, let's get down to business, creating and using pointers in C++.

How to declare pointers in C++: *

Just like any other type of variable, to use a pointer, we need to declare the pointer data type.

And it is done as follows:

  • type *name;

For example:

  • int *number;


This means that we create a pointer (as we put the asterisk in the declaration), which will point to a memory address that stores values of type int.

Be careful not to confuse: it is not because there is an int written that number is an integer. It's a pointer to an int, okay?

To avoid confusion, many programmers prefer to write:

  • float* value;


That is, it is not a float variable, it is a pointer to a float (it is float* and not float).
Run the code example below:
#include <iostream>
using namespace std;

int main()
{
    int *number;

    cout << number << endl;

    return 0;
}
The result is 0, which is the symbol of NULL or empty pointer, in C ++.
In fact, it is even a good practice to initialize with 0 or NULL, pointers.

Let's learn how to point the pointer to a more specific location.

How to Initialize Pointers in C++

Well, let's go.

First, let's create and initialize a variable of type double:

  • double pi = 3.14;


Now, let's declare a pointer that points to a variable of type double:

  • double *pointer;


To initialize a pointer we need to provide a memory address.
How about the memory address of the pi variable?
It is obtained like this: &pi

Then, declaring and initializing the pointer:

  • double *pointer = &pi;

There, our pointer points to a variable.
See the result of the code:

#include <iostream>
using namespace std;

int main()
{
    double pi = 3.14;
    double *pointer = &pi;

    cout << "pi variable value   : " << pi << endl;
    cout << "pi variable address : " << pointer << endl;

    return 0;
}

C++ e-book full PDF download for free

Pointer content: *pointer

We say that the variable pi directly references the value 3.14
The pointer variable indirectly references the value 3.14, as it points to the location where it is stored.

If we use the pointer variable, we are working with a memory address.
To handle the value that is stored at that address, we use: *pointer
That is, an asterisk before the name of the pointer.

Look:
#include <iostream>
using namespace std;

int main()
{
    double pi = 3.14;
    double *pointer;
    pointer = &pi;

    cout << "pi variable value   : " << *pointer << endl;
    cout << "pi variable address : " << pointer << endl;

    return 0;
}
That is, whatever does this:
cout << pi;

Or that:
cout << * pointer;

It is the same, because: pointer = &pi
That is, it doesn't matter which you use, because the pointer points to the variable.

How to use pointers in C++

And what is the purpose of that?
Simple: we can also change the value of the variables, using their pointers only.

See the example below, initially the variable has a value of 1.
Then, using a pointer that points to it, we change it to value 2:
#include <iostream>
using namespace std;

int main()
{
    int number = 1;
    int *pointer;
    pointer = &number;

    cout <<"number initial value: " << number << endl;
    *pointer = 2;
    cout <<"new number value    : " << number << endl;

    return 0;
}
In the previous example, it does not matter work with 'number' directly or with *pointer.
The asterisk, in this case, is called the indirect operator.

In the example below, we make the same pointer point (at different times) to different variables and change their values:
#include <iostream>
using namespace std;

int main()
{
    int a=1, b=1, c=1;
    int *pointer;

    cout <<"a+b+c: " << (a+b+c) << endl;
    cout <<"Incrementing a, b e c"<<endl;

    pointer = &a;
    (*pointer)++;

    pointer = &b;
    (*pointer)++;

    pointer = &c;
    (*pointer)++;

    cout <<"a+b+c: " << (a+b+c) << endl;

    return 0;
}
You see, we changed the value of 3 variables, without acting directly on them, with just one pointer.

Note that if we do: *pointer++, it will give an error, since the ++ operator takes precedence over the asterisk, that is, ++ will act before in the variable 'pointer', than the indirect operator *.

No comments:

Post a Comment