- Online Community Forums » Programming Questions » al_ref_ustr start and end effect

This thread is locked; no one can reply to it. rss feed Print
al_ref_ustr start and end effect
Member #23,119
May 2022

I'm a bit lost on how al_ref_ustr is supposed to work.
If it specifies an end point, I assume it's meant to only return chars until the specified end, right?

1ALLEGRO_USTR *text_typed = al_ustr_new("1234567890"); 2ALLEGRO_USTR_INFO text_info; 3 4int main() 5{ 6 al_init(); 7 printf("Char: %s \n",al_cstr(al_ref_ustr(&text_info, text_typed, al_ustr_offset(text_typed,2), al_ustr_offset(text_typed,4)))); 8 return 0; 9}

I expected:
Char: 34
I got:
Char: 34567890

If this isn't the case, which function should I use instead then?

Peter Hull
Member #1,136
March 2001

Your al_ref_ustr is fine, the problem is that you can't always convert a ustr to a cstr just with al_cstr. The reason is that al_ref_ustr has to create a substring from text_typed without altering text_typed, but a cstr is only a pointer to char, and the only way to figure out its length is to scan for the \0. So printf just runs past the end until it hits the end of the underlying string in text_typed.

You'd have to use al_ustr_to_buffer if you wanted a 'stand-alone' C string, which makes the whole substring thing a bit pointless maybe.

Obviously if you stick to ustr functions then the string made by al_ref_ustr would work perfectly.

Member #23,119
May 2022

That helped. I was using printf for troubleshooting in the original code, however. In the end, I wrote a very different code for external reasons though.
Thanks anyway!

Go to: