0
int p; long unsigned int z;
while (i <= x.length())
{
    const int a = x.length();
    char* b;
    b = x.substr(sizeof(a) - i, 1);
    p = atoi(b);
    z = (z + p + 3) * 3;
    i++;
}

I'm getting:

C:\Users\Anthony\Downloads\pack1.cpp|77|error: cannot convert 'std::basic_string<char>' to 'char*' in assignment|

I'm trying to go down backwards through 'x' and write each ascii code down as I go. The formula at the bottom is a hash. 'x' is a filename. I'll be unhashing it later. I need to run it through atoi().

Please help, as I do not know what to do. Everything else in the program is running fine, but as for this I'm a bit unsettled at the truthiness that this might be impossible. Please help, thank you.

David G
  • 94,763
  • 41
  • 167
  • 253
thexiv
  • 27
  • 9
  • Welcome to Stack Overflow. Please read the [About] page soon, and perhaps how to create an MCVE ([How to create a Minimal, Complete, and Verifiable Example?](http://stackoverflow.com/help/mcve)) now. The title mentions `itoa()`; the code uses `atoi()` and not `itoa()`? Which is what you intend? Where are `p` and `z` defined? – Jonathan Leffler Dec 29 '14 at 01:39
  • aslo why char pointer and why not just have char? – Irrational Person Dec 29 '14 at 01:39
  • 1
    `sizeof(a)` certainly doesn't belong in this *at all*. The implementation octet count of an `int` should have nothing to do with this. – WhozCraig Dec 29 '14 at 01:41
  • I used another answer here and it just gave me errors in my header. I did look around a bit before I asked. – thexiv Dec 29 '14 at 01:45
  • `(const char)b=x.substr(x.length()-i,1).c_str();` this makes it ask for an lvalue as the left operand. help? – thexiv Dec 29 '14 at 01:56
  • @thexiv You could also just pass the result of `x.substr(x.length()-i,1).c_str()` to `atoi` directly, IE `atoi(x.substr(x.length()-i,1).c_str())`, since the intermediate variable is just adding problems. – IllusiveBrian Dec 29 '14 at 02:00
  • @user3690202 that's a dangerous suggestion, because you'll be keeping a pointer to the internals of a temporary. – Mark Ransom Dec 31 '14 at 05:21

1 Answers1

2
int p; long unsigned int z;
while (i <= x.length())
{
  const int a = x.length();
  string b;
  b = x.substr(sizeof(a) - i, 1);
  p = atoi(b.c_str());
  z = (z + p + 3) * 3;
  i++;
}
user3690202
  • 3,844
  • 3
  • 22
  • 36
  • I'm looking for the way to get atoi to take bits of a string! There I said it right! W00t – thexiv Dec 29 '14 at 01:47
  • you answered the wrong question he wants ATOI not ITOA. – Irrational Person Dec 29 '14 at 01:49
  • @Bot - he originally asked about itoa. So it helps if he actually asks about what he wants. 2nd: try add a .c_str() to the end of your substr() line. I suspect that substr() returns a basic_string and not a char* – user3690202 Dec 29 '14 at 01:51
  • why don't you still change it? – Irrational Person Dec 29 '14 at 01:54
  • ` int i=1; for (i=0;i>=x.length();i++) { const char * b=x.c_str(); const char * c=b[i]; p=atoi(c); z=(z+p+3)*3; i++; }' this is what I have now, to make it clearer. – thexiv Dec 29 '14 at 02:06
  • @thexiv - Either way is pretty unclear, in my opinion. Plus, i'm pretty sure that the line "const char* c = b[i];" is just plain wrong. In either case, this answer is correct - you were missing the c_str() call. – user3690202 Dec 29 '14 at 02:17
  • You do a `c_str()` on a temporary, so `atoi(b)` is UB. – Jarod42 Dec 29 '14 at 02:35
  • 1
    With intermediate `b`, you have Undefined Behaviour as lifetime of the temporary ends once `b` is set (so `b` contains garbage at the next line (but the garbage may be correct)). With one line, you don't have UB. The safer way would be to use variable for the `substr` result. – Jarod42 Dec 29 '14 at 08:48
  • @user3690202: I have prepared for you an [example](http://coliru.stacked-crooked.com/a/ed9a3babbd7b4f30) which shows the UB – Jarod42 Dec 30 '14 at 02:30
  • @Jarod42 Apologies for my arrogant comment - you are completely right. substr() returns a basic_string, which gets destroyed after the call to c_str() which, as you rightly point out, leads to a dangling pointer. Silly me for not seeing that. – user3690202 Dec 30 '14 at 20:46
  • @user3690202: Now you may fix your answer. – Jarod42 Dec 30 '14 at 20:57