I know that you can take a pointer such as:
someFunction(const char* &txt)
{
txt++; //Increment the pointer
txt--; //Decrement the pointer
}
how do I get back to the beginning of the pointer? (without counting my number of increments and decrementing that amount) I was thinking about something like:
txt = &(*txt[0]);
But that isn’t seeming to work (assigning the pointer to its beginning location).
Any help would be greatly appreciated
Brett
You can’t. You have to either save it’s original position:
Or use an index:
Your attempt:
Is incorrect, if you look at it. First, it takes
txt[0](which is of typechar), then tries to dereference it with*, and then takes that address with the&operator (theoretically yielding the original result, though I wouldn’t want to think too hard about whether this works for dereferencing arithmetic types likechar), then assigns that (arithmetic) result to a pointer. It’s like you’re saying:It doesn’t make sense. What you probably were looking for was simply
which, at first glance, might look like it’s setting
txtto point to the first element oftxt, but keep in mind thattxt[0]is just*(txt + 0), or*txt. And&*txtsimplifies totxt, so you’re effectively doing:Or nothing. You get the same pointer. Use one of the two methods I described.