I am trying to split up an array (string1) of n characters into two character pointers representing i characters (char* first) and n-i characters (char* second) respectively. For the second array, I used
char* second = string1+n;
What I am wondering is how to use only the first i characters in first. I do not want to allocate more memory for the two arrays, I want to manipulate string1 so that I just point to parts of what is already there.
EDIT:
I cannot edit string1. Can I just cast first somehow to make the length shorter without adding a null character?
Unless you allocate more memory you can’t use the “first” as you would any other C string (e.g. passing it to string functions,
printf, etc) because it isn’t null terminated at the boundary with “second”.You can certainly still get around that in many cases because you know the length, but there’s nothing magical you can do here; the null terminator needs a byte.