only when I pass by reference which I don't understand |
Ok, the short answer is: because your function is recursive.
Since it moves the pointer through your string literal, it can’t be passed by copy, otherwise when it returns to the caller (which is itself), the caller would work on already managed characters.
Are your trying to implement some sort of calculator? I’ve been taken in by the name “morph()”, I guess.
Your code is fine, but you don’t need to access the pointed memory in many places; I mean, sometimes when you write
*expr++;
what you need is only to increment the pointer
expr++;
not to access the pointed memory.
If you used the subscript operator [] where you calculate your string literal length, you wouldn’t need to ‘restore’ your pointer later here:
for(int j=0;j<i+1;j++) *some--;
(I admit I couldn’t understand the purpose of that line).
Again, you don’t need indirection there.