Home Accessibility Courses Twitter The Mouth Facebook Resources Site Map About Us Contact
 
For 2021 - online Python 3 training - see ((here)).

Our plans were to retire in summer 2020 and see the world, but Coronavirus has lead us into a lot of lockdown programming in Python 3 and PHP 7.
We can now offer tailored online training - small groups, real tutors - works really well for groups of 4 to 14 delegates. Anywhere in the world; course language English.

Please ask about private 'maintenance' training for Python 2, Tcl, Perl, PHP, Lua, etc.
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 as below, or enter http://melksh.am/nnnn for individual articles
C206 - C and C based languages - Character Strings
  [1338] Handling Binary data in Tcl (with a note on C) - (2007-09-09)
  [2843] String functions in C - (2010-06-30)
  [2844] Learning about Regular Expressions in C through examples - (2010-06-30)
  [3122] When is a program complete? - (2011-01-06)
  [3144] Setting up arrays in C - fixed size at compile time, or dynamic - (2011-01-24)
  [3146] Strings in C - (2011-01-25)
  [3718] Splitting a record into individual data values in C - (2012-05-04)
  [4340] Simple C structs - building up to full, dynamic example - (2014-12-03)
  [4556] Strings in C - strncmp strncpy and friends - (2015-10-27)
  [4633] String handling in C - new examples of extracting integers from a string - (2016-01-27)


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
4759 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, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96 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., 2022: 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho

PAGE: http://www.wellho.net/mouth/3593_Cha ... alls-.html • PAGE BUILT: Sun Oct 11 16:07:41 2020 • BUILD SYSTEM: JelliaJamb