ustrtok_r(3)

πŸ“¦ version 4.4.3.1, in fedora 43, fedora 44

πŸ’– ustrtok_r(3) πŸ’–

okay so bestie, welcome to the Allegro manual ✨ this one's a lil function from the Allegro game programming library and honestly? she's giving "I can do it while other people are mid-sentence" energy. main character behavior fr fr πŸŽ€


✨ NAME ✨

ustrtok_r β€” the reentrant function that pulls tokens out of a string, brought to you by the Allegro game programming library. πŸ’…


πŸ¦‹ SYNOPSIS πŸ¦‹

#include <allegro.h>

char *ustrtok_r(char *s, const char *set, char **last);

🌸 DESCRIPTION 🌸

So ustrtok_r is literally the reentrant version of ustrtok πŸ‘‘ and the glow-up here is the last parameter, no cap.

That last guy is what keeps track of where the parsing is up to. It must be a pointer to a char * variable that you allocate, and it has to stay the same while you're parsing the same string. like, don't be switching it up mid-parse, that's not the vibe. πŸ™ˆ

Example time, bestie:

char *word, *last;
char string[]="some-words with dashes";
char *temp = ustrdup(string);
word = ustrtok_r(string, " -", &last);
while (word) {
   allegro_message("Found `%s'\n", word);
   word = ustrtok_r(NULL, " -", &last);
}
free(temp);

see how after the first call we pass NULL for s? yeah. that's her thing. once you're in the loop, the string lives in last's head, not yours. 🧠✨


πŸ’€ RETURN VALUE πŸ’€

She returns a pointer to the token, or NULL if there are no more tokens to find. periodt. 🫢

And here's the cute part: once you get that NULL back, you can free the memory that last is pointing to. it's done, it's retired, it ate and left no crumbs. πŸ“


🌈 SEE ALSO 🌈


Allegro β€” version 4.4.3 πŸ’œ

and that's the whole page bestie, mwah, go parse some strings, ily πŸŽ€βœ¨πŸ¦‹

πŸ“¦ where this page comes from