Retiring, March 2020 - sorry, you have missed our final public course.
The Coronavirus situation has lead us to suspend public training - which was on the cards anyway, with no plans to resume
Please ask about private 'maintenance' training for Python, Tcl, Perl, PHP, Lua, etc
Happily continuing private consultancy / programming work
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
, 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);
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 articlesC206 - C and C based languages - Character Strings 
String handling in C - new examples of extracting integers from a string - (2016-01-27) 
Strings in C - strncmp strncpy and friends - (2015-10-27) 
Simple C structs - building up to full, dynamic example - (2014-12-03) 
Splitting a record into individual data values in C - (2012-05-04) 
Strings in C - (2011-01-25) 
Setting up arrays in C - fixed size at compile time, or dynamic - (2011-01-24) 
When is a program complete? - (2011-01-06) 
Learning about Regular Expressions in C through examples - (2010-06-30) 
String functions in C - (2010-06-30) 
Handling Binary data in Tcl (with a note on C) - (2007-09-09)
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 upBack to UniChars, 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 CDefining, declaring and initialising variables in CPromoting a single one of your domains on the search enginesLearning 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
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).