0
#include <string>
using namespace std;

int main()
{
    string Str {"Stackoverflow"};
    unsigned char const *Ptr = &Str.front();
    unsigned char const *BufLim = &Str.back();

    cout<<ptr;
    cout<<BufLim;

    return 0;
}

The above code is not compiling, I'm getting:

error: invalid conversion from ‘__gnu_cxx::__alloc_traits, char>::value_type*’ {aka ‘char*’} to ‘const unsigned char*’

I want to assign a string's initial character and the last character to a pointer of type unsigned char const.

Kindly help me on this.

Remy Lebeau
  • 505,946
  • 29
  • 409
  • 696
Meganathan
  • 21
  • 5
  • 2
    As the compiler says ***error: invalid conversion***, it is not possible, unless you use `reinterpret_cast`, [for which you should have strong reasons](https://stackoverflow.com/questions/573294/when-to-use-reinterpret-cast). BTW, why not to use `const char*` or `const char *const`or simply `char*`, which are suitable here? – JeJo Jun 03 '22 at 06:38
  • @churill Ouch! You're correct. My fault! (What a shame.) (I could swear I've seen `char * const`... Maybe, I need new glasses.) ;-) – Scheff's Cat Jun 03 '22 at 06:41
  • 1
    @Scheff'sCat Yep, it's quite easy to confuse this pointer madness, no reason to bea ashamed. :) – churill Jun 03 '22 at 06:47
  • You could trust `auto` – Goswin von Brederlow Jun 03 '22 at 14:39

0 Answers0