String handling in C seems challenging (compared to most languages which aren't Assembly :-P ). Anyone got any good tips that will save me lots of trouble when I work with strings in C? Are there better ways to implement strings in C besides that standard NUL-terminated one?
you can also implement strings using pointers.
how?
Always always always use the *n* functions. So strncat, strncomp, etc. These take explicit lengths for their actions, and ensure that you don't accidentally overflow the end of your string. Other than that, yeah, strings can be a pain in C. Just the way of things :) (Implementing strings using pointers -> just remember arrays are a very thin veneer on pointers. You're already using strings as pointers ;) )
I know this is basic, but IMHO, when it's overlooked life becomes difficult: C doesn't have strings, it has array of char. Any of the "string" functions are iterating over an array.
@rsmith You're absolutely right, unfortunately :( and the only support C has for strings would be converting string literals in code into NUL terminated arrays of characters, and string.h for basic manipulation of NUL terminated strings.
Join our real-time social learning platform and learn together with your friends!