Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
Chars, char arrays and strings in C. Some early cautions and pitfalls.

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 (\0).

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.
(written 2012-01-26, updated 2012-01-28)

 
Associated topics are indexed under
C206 - C and C based languages - Character Strings
  [3146] Strings in C - (2011-01-25)
  [3144] Setting up arrays in C - fixed size at compile time, or dynamic - (2011-01-24)
  [3122] When is a program complete? - (2011-01-06)
  [2844] Learning about Regular Expressions in C through examples - (2010-06-30)
  [2843] String functions in C - (2010-06-30)
  [1338] Handling Binary data in Tcl (with a note on C) - (2007-09-09)


Back to
Are we one man, one vote? No, and we never have been.
Previous and next
or
Horse's mouth home
Forward to
Back to Uni
Some other Articles
Niche businesses in Melksham. From glass painting to Tcl and Lua courses.
Want to learn iPad and iPhone programming? Come along and learn with me for free.
Looking up
Back to Uni
Chars, char arrays and strings in C. Some early cautions and pitfalls.
Are we one man, one vote? No, and we never have been.
Integer types, and integer overflows, in C
Defining, declaring and initialising variables in C
Promoting a single one of your domains on the search engines
Learning about how to help people learning - and retaining
3610 posts, page by page
Link to page ... 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73 at 50 posts per page


This is a page archived from The Horse's Mouth at http://www.wellho.net/horse/ - the diary and writings of Graham Ellis. Every attempt was made to provide current information at the time the page was written, but things do move forward in our business - new software releases, price changes, new techniques. Please check back via our main site for current courses, prices, versions, etc - any mention of a price in "The Horse's Mouth" cannot be taken as an offer to supply at that price.

Link to Ezine home page (for reading).
Link to Blogging home page (to add comments).

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2012: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 899360 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho

PAGE: http://www.wellho.net/mouth/3593_Cha ... alls-.html • PAGE BUILT: Fri Feb 3 14:16:04 2012 • BUILD SYSTEM: wizard