Skip to content

Instantly share code, notes, and snippets.

@derekli66
Last active September 6, 2015 13:45
Show Gist options
  • Save derekli66/b4f1d1327d5807af5fed to your computer and use it in GitHub Desktop.
Save derekli66/b4f1d1327d5807af5fed to your computer and use it in GitHub Desktop.
(Weird result so far...) Rewind a C string from the last pointer to the first pointer which the start of the string.
char *rewind_string(char *last_char) {
// Prevent NUL byte.
if (*last_char == '\0' || last_char == NULL) {
return NULL;
}
char *first_char;
// Rewind to the pointer pointing to first character.
for (first_char = last_char; *first_char != '\0'; first_char--);
return (*first_char == '\0') ? (first_char + 1) : first_char;
}
@derekli66
Copy link
Author

I would like to rewind a string pointer from pointing to last character to first character. But, the result is a little wired because extra characters were found in front of the string.

It seems that precise rewinding has to use the string length to prevent undesired characters being found.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment