1

in my below program i get compilation error

error: comparison between distinct pointer types 'unsigned char*' and 'const char*' lacks a cast

How to tackle this and do comparison

#include <iostream>
#include <vector>

using namespace std;

std::vector<std::uint8_t> vec;

void Init(void* tmp)
{
    auto dd = static_cast<std::uint8_t *>(tmp);
    std::cout<<"loop : "<<dd<<std::endl;
    if(dd == "BBBB_DDDDD_XXUSTYY_99_7DFFXX9B67")
      std::cout<<"equal";
    else
      std::cout<<"not equal";
}

std::string PadIt(std::string& str, std::size_t outputLength)
{
    if (outputLength > str.size()) {
        char paddingChar = ' ';
        str.insert(str.size(), outputLength - str.size(), paddingChar);
    }
    std::cout<<"PadIt str is : "<<str<<" size is: "<<str.size()<<std::endl;
    return str;
}  

int main() {

    std::string key_str = "BBBB_DDDDD_XXUSTYY_99_7DFFXX9B67";
    std::string obid = PadIt(key_str,16);
    std::vector<std::uint8_t> vec(obid.begin(),obid.end());
    Init(vec.data());

  
}

I want to do comparison of string in Init function, if a wrong string is given it show give false

ganeshredcobra
  • 1,881
  • 3
  • 28
  • 44
  • What exactly are you intending to do with the comparison `if(dd == "BBBB_DDDDD_XXUSTYY_99_7DFFXX9B67")`? – UnholySheep Oct 20 '22 at 10:49
  • 1
    Why do you cast to `std::uint8_t *` when you intend to compare to a `char*`? – molbdnilo Oct 20 '22 at 10:50
  • That comparison will *never* be `true`, unless you pass in a pointer to the exact same literal string. – Some programmer dude Oct 20 '22 at 10:50
  • This needs elaboration of what is the *problem* this is supposed to solve, because frankly it has a design aroma that isn't exactly pleasant. You should update your post to include what this is intended to do, and *why*. – WhozCraig Oct 20 '22 at 10:53
  • The comparison is pointless - the location of the first element of a `std::vector` can't be the same as the location of the first element of an array (or any other object). – molbdnilo Oct 20 '22 at 10:57
  • @molbdnilo thanks i tried giving auto dd = static_cast(tmp); then there is no compilation error, but it false to not equal to why is that. How can i accomplish a succeful comparison here – ganeshredcobra Oct 20 '22 at 10:57
  • Just a warning: even `"aaa" == "aaa"` might return false. `"aaa" == ` is __guaranteed__ to return false. – Revolver_Ocelot Oct 20 '22 at 10:58
  • 1
    If you want to compare strings, why are my messing about with `std::vector` and `void*`? I think one of [these](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) would be good for you. Read in particular about arrays, pointers, array-to-pointer decay, and the differences between "C-style" strings and `std::string`. – molbdnilo Oct 20 '22 at 10:59
  • What is the purpose of the `Init` function by the way. It doesn't seem to actually be doing any kind "init" or initialization. Good naming is very important to make code easier to read, understand, debug and generally maintain. – Some programmer dude Oct 20 '22 at 11:02
  • Also, [magic numbers](https://en.wikipedia.org/wiki/Magic_number_(programming)) are bad, the same goes for magic strings. – Some programmer dude Oct 20 '22 at 11:03
  • @molbdnilo my actual project case need these datatype conversions – ganeshredcobra Oct 20 '22 at 11:28
  • And be careful: The data you store in the vector is *not* a null-terminated string. Any attempt to use that data as a null-terminated string will fail. – Some programmer dude Oct 20 '22 at 11:32
  • I figured out it works like this std::string data = "BBBB_DDDDD_XXUSTYY_99_7DFFXX9B67"; std::string dd = static_cast(tmp); if(dd == data) std::cout<<"equal"; else std::cout<<"not equal"; – ganeshredcobra Oct 20 '22 at 11:36
  • A working result - https://godbolt.org/z/TWexv6G9z – ganeshredcobra Oct 20 '22 at 11:38

0 Answers0