say I declare a char pointer in the following way:
char *str = "123456";
and I want to point to the sub string "123"
, how do I decrement the pointer so that the pointer str
points to 123?
I know I can increment the pointer to point to the latter part of the string:
// now str will point to 456
str+=3;
but what about the beginning of the string/trimming the end of str
string using the decrement --
/-=
operator, is this possible?
I think you are somehow already pointing to the string 123....the only differene is that it is null terminated at the 7th character, after the '6' and therefore you get the string 123456
If you are looking for a "left" method, to take only the initial 3 characters, probably you need to have a look to the function strncpy. As an alternative, you can also terminate the string by putting the 4th character to null. In the second way anyway, you are altering the original content you had and you are losing information.