Tutorial 14 on working with strings published

Code listing on screen with keyboard
Image by Markus Spiske from Pixabay

Reading the C reddit most days, I see from time to time question about strings. They’re not particularly complex but I feel you really have to get your head around pointers to grok strings. I’ve already done a C tutorial on Pointers and c-strings but I thought showing some examples of doing things with c-strings would not go amiss.

The pointers aspect of C strings probably muddies the water a bit. All you are doing is manipulating a contiguous block of characters in RAM that ends with a 0. The pointer just tells you where in RAM that block begins. So long as it finishes with a 0 (\0 in C), it will work ok.

That’s what Tutorial 14- working with strings is about.  Especially for beginners, doing things like converting ints to strings, or concatenating strings can be a bit fiddly. I’ve also provided both the Windows and Linux versions. i.e. compiling with Visual C on Windows and clang on Ubuntu.

For instance, you might have heard about the long to ascii function ltoa. Bad news. It doesn’t exist in Linux compilers.  There is a Windows version with the instantly memorable (not) name _ltoa_s, which is one of the safe C functions.  Incidentally if you are using SDL then there is a SDL_ltoa function provided for you although oddly it’s not documented.

(Visited 9 times, 1 visits today)