Can i have something like a reference to pointer that can change the pointer but not the value in the pointed location.
Code:void fail(const char *& i) { i++; } int main() { char s[]="something"; char *j=s; fail(j); }
Can i have something like a reference to pointer that can change the pointer but not the value in the pointed location.
Code:void fail(const char *& i) { i++; } int main() { char s[]="something"; char *j=s; fail(j); }
I just changed it to
const char *j=s;
and it does what I expected it to.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
the code you posted will increment the pointer itself. following the call to fail() in main(), j will now point to the 'o' in "something." the traditional (C) way of doing this was to pass a pointer to the pointer (char**), but C++ allows a reference to a pointer (but not the other way around), so you can modify the pointer itself without needing to dereference anything.