Ask coding questions

← Back to all posts
why doesn't this work?
nxzqw (1)

it's been coded with no errors in logic or grammar, yet it says it doesn't work...

Answered by xxpertHacker (860) [earned 5 cycles]
View Answer
Voters
nxzqw (1)
Comments
hotnewtop
xxpertHacker (860)

ptrtostring is a null pointer, plain and simple.

If it is not a null pointer, then it is uninitialized, garbage data, which is arguably worse ;)

Maybe you wanted

ptrtostring = &iee;

Ideally you'd not use that useless pointer at all, in favor of just using iee.

for ( decltype(auto) character : iee ) {
    std::cout << character << '\n';
}
nxzqw (1)

@xxpertHacker, you help me out with this. i was puzzled with this stupid simple contraption, but you helped me fix it :D

Coder100 (17087)

unsigned is not a complete type.

#include <iostream>
#include <string>

using namespace std;

int main() {
  string iee = "hello";
  string *ptrtostring = &iee;
  for (unsigned int i = 0; i < iee.length(); i++) {
    cout << (*ptrtostring)[i] << endl;
  }
}
xxpertHacker (860)

@Coder100 unsigned, when not used as a qualifier, defaults to unsigned int.

constexpr unsigned MAX = -1u;

Perfectly valid (although, I don't advocate it).

Regardless, irrelevant to the real problem, refer to my answer.

InvisibleOne (2677)

It says it's a segmentation fualt, which is something to do with trying to access read only or write only memory location.
https://stackoverflow.com/questions/2346806/what-is-a-segmentation-fault

Coder100 (17087)

imagine not being able to write to memory that was meant to be written over xd @InvisibleOne