String Manipulation when using the C API


Hi all,

We received our playdate a while back and I’ve been happily playing with it off and on ever since!

I know C better than I know Lua so I went straight for that. I didn’t have much luck getting anything building in Windows for C including the examples but I did have more success on the Mac, so I swapped over to using Xcode to build and have started making a little game.

I tried to use snprintf to put an integer into a string before displaying it. This gave me a compile error and after a bit of investigating it seems as though there probably just isn’t support for that method?

FYI the error read: undefined reference to `_sbrk’

I’ve searched the forums and it seems like most people are using Lua which has string manipulation built in. Perhaps I should have just gone straight for Lua but I thought I’d be more comfortable in C!

Any help is much appreciated, perhaps there is a more obvious way that I’ve overlooked? Right now I’m thinking that I need to write my own method for stringifying a number?

This stumped me for a bit as well! The good news is that you don’t need to write it yourself, the SDK includes a format string API:

// make a formatted string
char* str;
pd->system->formatString(&str, "Hello, world! %d", 123);

// (do stuff with str)

// release it
pd->system->realloc(str, 0);

(It’s worth noting that other string functions, like strlen, are supported)

It’s definitely worth looking through the “Inside Playdate with C” doc as well as the C API header files when you get stuck.

Oh cool, thanks so much. Yeah, I tested the other string functions worked ok and I could see that it was to do with the fact that function needs to allocate. Also, I read through “Inside Playdate with C” but I guess not thoroughly enough!

Thanks so much for your help!

1 Like

Mistermo already answer your question but related to your _sbrk issue I was able to fix it in a project by adding a compiler option.

In the cmakelists.txt I’ve added
add_link_options(-specs=nano.specs -specs=nosys.specs)

Not sure if that would make it possible to use snprintf though…

1 Like

Thanks for the additional info Nic!
I had also found that linker option whilst searching but couldn’t work out where I needed to put it. Building with makefiles also not something I’ve spent a lot of time doing, not recently at least!