Showing posts with label *. Show all posts
Showing posts with label *. Show all posts

Pointers in C++ - What they are and Memory Addresses (& operator)

In this introductory tutorial to our section on pointers in C++, we will learn the basics of this important concept.

Pointers - What are they? What are worth for? Where they live? What do they feed on?

Pointer is nothing more than a variable, just like int is, like float, double, char etc.
And like every variable, it serves to store a special type of data.
In the case of pointers, these variables are used to store memory addresses.

We are going to use these variables to point to certain memory locations, and work on that.
For example, if we do:

  • int count = 1;


The count variable is stored somewhere in memory, and there is a value of 1.
We could use a pointer to point to the address of this variable. Inside the pointer it does not have the value 1, but a memory address, which points to a location where the 1 is stored.

When we make a statement like this, we are separating a certain amount of kiloBytes in memory to store that value.

Memory addresses: operator &

Still using the example variable, where we use count, C++ replaces it with its value, 1.
For example:

  • cout << count + count;

It will print 1 + 1, that is, 2.

However, it is possible to work with the address, the space in memory, where this number 1 is allocated, instead of working with the value it stores, to do so just use the symbol & before the variable:

  • & count



For example, the code below declares a variable, stores a value.
Then, print the value of the variable, the amount of memory that has been reserved (using the sizeof function, which returns the number of kBytes used) and finally, show its address, using the & operator:

#include <iostream>
using namespace std;

int main()
{
    int count=1;

    cout<<"Variable value  : "<< count << endl;
    cout<<"Size in KB      : "<< sizeof(count) << endl;
    cout<<"Memory address  : "<< &count <<endl;

    return 0;
}

See, on our machine, the int variable uses 4 KB and was stored at the address:


And on your machine?
Now a challenge: how many KB are used to store a memory address?
Write in the comments.

And what does this weird memory address number have to do with a pointer?
Very simple: just as int variables store integers, char variables store characters, etc., pointers are variables that store this thing: memory addresses.

That is, the pointer will point to a memory location, and it is possible to do incredible things with this super simple idea, such as dynamic memory allocation, which we will see later in our C++ course.

C++ Math: Operators of sum (+), subtraction (-), multiplication (*), division (/), and remainder of division (%)

Without too much hype or oversimplification, we can say that a computer is nothing but a mega super hyper calculator. A special, well-incremented and powerful calculator.

Doing calculations, counting ... is the basis of computing, and hence of programming.

In this tutorial, we will learn how to do C ++ accounts.

C++ sum: + operator

To perform the addition operation, we use the + operator between two values.
It can be in literal form: 1 + 1

Or values stored in variables: value1 + value2

A program that shows the sum of the numbers 21 and 12:

#include <iostream>
using namespace std;

int main()
{
    cout << 21 + 12;
    return 0;
}


Sum + operador in C++

These values could be previously stored in variables, var1 and var2, for example and summed later, see:

How to sum in C++

Notice how we declare the variables var1 and var2.
Since both are integers, we do not need to do:
int var1=1;
int var2=2;

We can 'summarize', write less and do:
int var1=1,
     var2=2;

Much more fancy, isn't it?

C++ Subtraction: - operator

As we add, we can subtract, and for that, just use the - (minus) operator.
Let's subtract 21 from 12, and then do 1 minus 2:

#include <iostream>
using namespace std;

int main()
{
    int var1=1,
        var2=2;

    cout << "21 - 12 = " << 21-12 << endl;
    cout << "1 - 2 = " << var1-var2;
    return 0;
}

See the resulto:
Minus operation in C++

Here we use strings:
"21 - 12 = "
"1 - 2 = "

Here, numbers, math values:
21 - 12
var1 - var2

It's two different things, it's two different data types.

We did this and put everything together in the cout command (simple C++ output), to make the output very cute and neat.

C++ multiplication: operator

In our day to day, to describe the multiplication operation, we use the x symbol, sometimes, true?
However, in C++ programming, 'x' is a letter.

To identify the product operator, we use the asterisk: *

So to multiply 2 by 2, or 21 by 12, we do:

#include <iostream>
using namespace std;

int main()
{
    int var1=2, var2=2;

    cout << "2*2 = " << var1 * var2 <<endl;
    cout << "21 * 12 = " << 21*12;
    return 0;
}

Compilation result:
How to calculate product in C++

Note that we declare the variables all on the same line. We did this because they are the same type, the integer type, and separated by comma.

We could also have done:
int var1 = 2;

And then: var1 * var1

After all, are of equal values, agree?

C++ division: operator

In daily life, the division operator is also different, it is the “÷”.
In programming, the symbol is: /

Let's divide 4 by 2, and then 1 by 3.
The code is:

#include <iostream>
using namespace std;

int main()
{
    float var1, var2;
    var1=1;
    var2=3;

    cout << "4 / 2 = " << 4/2 <<endl;
    cout << "1 / 3 = " << var1/var2;
    return 0;
}

The compilation results in:
How to divide in C++ programming language

Note that we first declare the variables:
float var1, var2;

And only then we initialize with the values 1 and 3:
var1 = 1;
var2 = 3;

It is another way of declaring and initializing variables.

In the first cout, we divided 4 by 2. Two integers, with results of integer division, and C++ showed the resulting integer: 2.

But by dividing 1 by 3, the result is decimal. So we declared the variables var1 and var2 as float, because we knew we would need the decimal places to represent the division operation.

Change the float declaration to double, what happened? Say it in the comments.
Change the float declaration to int, what happened? Say it in the comments.

Remainder: % operator


Finally, let's use the% arithmetic operator, called the modulus or remainder of the division.
Let's go back to school and remember how we used to contain:

Rest of division in C++

See that 'remainder'? It is the rest of the division of 17 by 2.

To get this result, do:

  • 17 % 2 = 1
This operator is special because it's only used with integers, ok?
In the future, in our C++ course, we will use the module operator (or remainder of the division) for some specific algorithms, such as finding prime numbers and working with multiples.


Math exercises in C++

01. Write a program that adds the numbers 10, 20 and 30
02. Write a program that divides 21 by 12
03. What is the module, or remainder of the division, of number 21 by 12?
04. What is the error of the code below?


#include <iostream>
using namespace std;

int main()
{
    number = 2112;
    int number;

    cout << "Number: "<< number <<endl;
    return 0;
}

05. Store in the 'sum' variable the result exercise 1. Then divide by 3 to find the average, what value did you find?
06. Redo the previous exercise, but without using any variables, that is, print the value of the direct result in cout.
07. If you do: 10 + 20 + 30/3, what is the result? Is it the average? If not, why did it go wrong?

Soon, still in our Introduction to C++ section, let's return to the operators in relation to their precedence.

Study resources