Last active
September 6, 2015 13:45
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.