K
K
kos_dev2021-10-03 13:36:12
C++ / C#
kos_dev, 2021-10-03 13:36:12

How to put a type in unsigned char NULL and after successfully catch a successful comparison with another unsigned char or char?

Hello. A two part question.

First part:
What is better to put in the unsigned char type in order to use it as an empty value. When NULL is set, the '\0' sequence is stored in the variable, I'm not sure that this is the best option, you never know how it can be stored in a similar variable in some way, when entering data into it or when declaring it.
I do something like this:

I assign a variable unsigned char NULL value
unsigned char ch = NULL; // '\0'


Second part:
I can't successfully compare two unsigned char variables in any way, I tried to explicitly convert them to int, I tried to compare unsigned char and char, nothing works, although the variables contain an identical value, for example
'n' and 'n', but comparing them returns false.
Examples of unsuccessful comparisons:
Trying to compare unsigned char and unsigned char
unsigned char[255]; 
vector<unsigned char> text;
// Далее через цикл каждое значение делаю NULL, char[i] = NULL;
// После идут пара циклов, в которых происходит сравнение
if(char[i] == text[j]) cout << "Успех, переменные имеют одинаковое значение";
else cout << "Неудача";

An attempt to compare with char, which we pull out from string
unsigned char[255]; 
string text;
// Далее через цикл каждое значение делаю NULL, char[i] = NULL;
// После идут пара циклов, в которых происходит сравнение
if(char[i] == string[j]) cout << "Успех, переменные имеют одинаковое значение";
else cout << "Неудача";

Compare by explicit cast to int
unsigned char[255]; 
string text;
// Далее через цикл каждое значение делаю NULL, char[i] = NULL;
// После идут пара циклов, в которых происходит сравнение
if((int)char[i] == (int)string[j]) cout << "Успех, переменные имеют одинаковое значение";
else cout << "Неудача";

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Armenian Radio, 2021-10-03
@kos_dev

Especially for this, in C ++, starting from version 17 of the standard, std:: optional
was brought in. This thing allows you to store a value inside yourself or remember that the value is not currently set.
Example:

#include <optional>
using ochar = std::optional<char>;

ochar a = 'a';
ochar b;

if(a) { std::cout << " a exists and contains " << *a << std::endl;}
if(!b) {std::cout << " b does not exist" << std::endl;}

Example from the manual:
#include <string>
#include <functional>
#include <iostream>
#include <optional>
 
// optional can be used as the return type of a factory that may fail
std::optional<std::string> create(bool b) {
    if (b)
        return "Godzilla";
    return {};
}
 
// std::nullopt can be used to create any (empty) std::optional
auto create2(bool b) {
    return b ? std::optional<std::string>{"Godzilla"} : std::nullopt;
}
 
// std::reference_wrapper may be used to return a reference
auto create_ref(bool b) {
    static std::string value = "Godzilla";
    return b ? std::optional<std::reference_wrapper<std::string>>{value}
             : std::nullopt;
}
 
int main()
{
    std::cout << "create(false) returned "
              << create(false).value_or("empty") << '\n';
 
    // optional-returning factory functions are usable as conditions of while and if
    if (auto str = create2(true)) {
        std::cout << "create2(true) returned " << *str << '\n';
    }
 
    if (auto str = create_ref(true)) {
        // using get() to access the reference_wrapper's value
        std::cout << "create_ref(true) returned " << str->get() << '\n';
        str->get() = "Mothra";
        std::cout << "modifying it changed it to " << str->get() << '\n';
    }
}

An important difference from using, for example, a pointer - optional does not make dynamic memory allocations, the value is stored (or not stored) right inside it.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question