Tutorial seven on pointers and C strings published
The tutorials from About.com continue with the 7th one (of about 30) published. This is about C strings which are really just pointers to an array of characters. Once you understand pointers strings are easy enough to understand.
C is not a great programming language for string handling. To do a lot of manipulation is tedious and error prone. You’ll find safe versions of many of the standard functions for things like string copying and appending. The difference between the safe functions and the non-safe functions is that the safe functions include a maximum length.
For example strcpy() is used to copy a string. It’s definition is this:
char *strcpy(char *dest, const char *src)
That is, it copies a string pointed to by src to a string pointed by dest and confusing also returns a pointer to dest. What a waste of a function. It could have returned an int saying how many characters were copied instead. Because it relies on src pointing to a string (char *) that terminates with a null (or 0). If the null is missing it can copy a lot more characters and that’s how buffer overflow bugs happen. So you have strncpy which is defined as this:
char *strncpy(char *dest, const char *src, size_t n)
The extra parameter is how many characters are to be copied. That way if it goes wrong, it is limited to n.
The picture? That’s a different kind of sea string…<groan>