Main Content

Chars, char arrays and strings in C. Some early cautions and pitfalls.

Archive - Originally posted on "The Horse's Mouth" - 2012-01-26 11:34:12 - Graham Ellis

A char in C is a single byte variable, and a string in an array of chars (i.e. a series of chars held in successive memory addresses) and terminated by a null (%body%).

Because of this need for a terminator, you need to allocate one ADDITIONAL byte of memory / character position than the maximum number of characters you'll be holding in a string, and failure to do so will lead to a variable leak where you go off the end of your string into whatever happens to be stored next in memory. You may be very lucky and get away with it, you may be quite lucky and pick up your problem immediately in testing, or you may write a piece of code which passes your tests, but fails inexplicably from time to time and gets itself a dreadful reputation for being unreliable.

You also need to ensure when you're manipulating strings that the code you write correctly adds an terminating nulls that you need - otherwise when you use the %s formatter in sprintf or printf, you'll get the string that you intended and that will be followed by whole series of apparently random extra characters.

Here's what I mean. Code:
  char capital = 'A';
  printf ("The letter is %c.\n",capital);
  printf ("The string is %s.\n",&capital);


Result:
  The letter is A.
  The string is A??_?.


Source / full example at [here].

There are plenty of string handling functions in C to help you do what you need - but you'll need to understand what's going on under the covers too in order to ensure that you make good, clean, error free use of them.