Showing posts with label >. Show all posts
Showing posts with label >. Show all posts

Comparison of Pointers and Pointer to Constants

In this tutorial, we will learn how to compare pointers as well as how to use constant variables with them.

Comparison between pointers in C++

In the same way that we can compare any two variables (such as int and double), we can also compare pointers.

And using the same operators:>,>=, <, <=, == and !=

We say, for example, that one pointer is greater than another, when, for example, its memory address in an array points to a variable whose index is greater than another.

For example:
int *ptr1 = array[0];
int *ptr2 = array[1];

So, the comparison: ptr2 > ptr1 will result in a true result.
ptr1 == ptr2 will result in a false result, as they point to different memory addresses.

That is, pointers are variables that store memory addresses.
So, when comparing two pointers, we are comparing two memory addresses, and not the values they point to, ok?

Pointers and C++ const

Whenever we use the keyword const, we are telling the compiler that the value stored in that variable should not be changed.

Often, we want to pass a variable as information for somewhere in code, but we don't want it to be modified in any way, in these cases, it is important to use the keyword const.

So far, we have used non-constant pointers that point to non-constant variables, that is, we can change up to the value of the variable via a pointer that points to it.

You cannot, for example, pass a pointer, which is not constant, to a function that expects a constant variable as an argument. Also, look:
#include <iostream>
using namespace std;

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

    cout << *ptr << endl;

    return 0;
}
To point to a constant variable (pi), we had to define a constant pointer (const double *).
Try to remove the 'const' from the pointer declaration, and see what happens.

However, we can have a constant pointer that points to a variable that is not constant:
#include <iostream>
using namespace std;

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

    cout << *ptr << endl;

    return 0;
}
The difference is that this pointer will ALWAYS point to the same memory address. You can even change the value it points to. But he will never change the address it points to. Constant type pointers must be initialized when they are declared.

Finally, we can have a constant pointer, which points to a constant variable:
#include <iostream>
using namespace std;

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

    cout << *ptr << endl;

    return 0;
}
Note that, in this case, we cannot change the address of the pointer (we cannot do ptr =&another_variable) nor can we change the value stored where the pointer points (*ptr = 21.12)

These cases of constant and pointers, as well as the comparison between pointers, are used a lot in the study of strings, for example.

Relational (comparison) operators in C ++:>, <,> =, <=, == and !=

In this tutorial of our C++ Course, we will learn what they are and how to use relational operators, also known as comparison operators, which are:
  • >    greater than
  • <    less than
  • >=  greater than or equal
  • <=  less than or equal
  • ==  equal
  • !=  different

How to compare things in C++

Although this tutorial is a bit boring and theoretical, it is necessary to better understand the use of the IF and ELSE conditional tests, which we will use in the next tutorials.

What we are going to do now is teach you how to perform the comparison operation, it is done like this:
  • operand1 operator operand1

That is, we will use two operands (data, such as numbers or characters) and one operator (in this case, a relational).

And this operation always returns one of the values:
  • TRUE (true or 1)
  • FALSE (false or 0)

Ever.
Relation operation is always a question that the answer is 1 or 0, true or false.
OK?

Equality operator in C++: ==

Well, let's go.
If we want to know if an x value is equal to a y, we do:
x == y

You see, it's: x == y
And no: x = y

Let's test if 1 equals 1 and then if 2, run the code below:
#include <iostream>
using namespace std;

int main()
{
    cout << (1 == 1) << endl;
    cout << (1 == 2) << endl;

    return 0;
}
The result was:
C++ free online course and tutorials
Actually, 1 equals 1 (1, TRUE, the result of the comparison).
And 1 is not equal to 2 (0, FALSE, the result of the comparison).

Not equal operator in C++: !=

Just as you have one operator that checks for equality, so has another that checks for non-equality.
If two values are different, the comparison operation with != will result in TRUE.

If they are equal, it results in FALSE.

Let's see in practice, compile and run:
#include <iostream>
using namespace std;

int main()
{
    cout << ('a' != 'b') << endl;

    return 0;
}
That is, the character 'a' is different from 'b'.
Now test if 'a' is different from 'A'.
IT'S ?

Greater than operator C++: >

If we want to test if an X value is greater than a Y value, we use the operator: >
X> Y

If X is larger, it returns TRUE (1).
If equal or less, it returns FALSE (0).

Greater than or equal operator in C++: >=

If we want to test if a value is greater than or equal, we use: >=
X >= Y

It returns 1 (true) if X is greater than or equal to Y, and 0 if X is smaller.

For example, to test if a person can already drive, drink, enter the motel ... we should do:
age >= 21

Lessa than and less than or egual to operators C++: < and <=

Similarly, there are the smaller and smaller operators equal.

To test if a person can donate blood, we must test:
age < 65

To find out who is exempt from tax returns, we should test:
value <= 28559.70

Beware of comparisons

A very serious mistake is to confuse:
x = y;

With:
x == y;


In the first case, it is an assignment.
Variable x is receiving the value of variable y.

In the second case is a comparison.
Always read comparison as a question: x is equal to y?

And when you ask C ++ things, it only gives you two answers: 1 or 0
So if you want to store the value of a comparison, declare a boolean, ok?

  • C++ exercise

Make a program that prompts the user for two values (can be a number or a character), then display all comparison operations, as in the example below:

C++ tutorial

Did you try?
Worked?

Our code looks like this:
#include <iostream>
using namespace std;

int main()
{
    int op1, op2;

    cout << "First value  : ";
    cin >> op1;

    cout << "Second value : ";
    cin >> op2;

    cout << op1 << " >  " << op2 <<" : "<<(op1>op2)<<endl;
    cout << op1 << " <  " << op2 <<" : "<<(op1<op2)<<endl;
    cout << op1 << " >= " << op2 <<" : "<<(op1>=op2)<<endl;
    cout << op1 << " <= " << op2 <<" : "<<(op1<=op2)<<endl;
    cout << op1 << " == " << op2 <<" : "<<(op1==op2)<<endl;
    cout << op1 << " != " << op2 <<" : "<<(op1!=op2)<<endl;
    return 0;
}
And yours?