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.
Strings in C - strncmp strncpy and friends

Strings in C are pointers to arrays of characters, null (\0) terminated. Which means that if you compare them with the == operator, you're going to be comparing addresses, and not whether the strings contain the same text. And even if you put a "*" in front of the variable you're comparing, all you'll do is compare the first character.

Solution is to use standard functions such as strcmp and strcpy - string compare and string copy. Both of these are regarded as dangerous as they can run off the end of stings that aren't properly null terminated, so you also have strncmp and strncpy which are better regarded, and have an extra parameter which is the maximum number of characters to compare / copy.

Similarly, fgets to read in a string (use stdin for the keyboard) with a maximum length is much safer that gets which can easiiy be overrun by a too-long input!

Be careful too of strcmp and strncmp - they return 0 if the strings are equal - that's a false value and the opposite of what you would expect ... and they return +1 if the first value is logically (ascibetically) before the second, and -1 if it's after. Once you realise this is done to help you sort / order strings, it's logical - but it catches newcomers out all the time!

Example [here] from yesterday's C course - see [here].
(written 2015-10-27)

 
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)
  [3593] Chars, char arrays and strings in C. Some early cautions and pitfalls. - (2012-01-26)
  [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)
  [4633] String handling in C - new examples of extracting integers from a string - (2016-01-27)


Back to
Preprocessor directives in C and C++ - what they mean
Previous and next
or
Horse's mouth home
Forward to
Function prototype - what they are and why you should use them - C and C++
Some other Articles
Variables, Pointers and References - C and C++
When do I use the this keyword in C++?
Well House Consultants - Python courses / what's special.
Function prototype - what they are and why you should use them - C and C++
Strings in C - strncmp strncpy and friends
Preprocessor directives in C and C++ - what they mean
Passing information into functions in C - by name, by value
RUby - loading, using, changing, storing JSON format data
Scenario outlines - tables of values to test - in Gherkin / Cucumber
Testing your new class - first steps with cucumber
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/4556_Str ... iends.html • PAGE BUILT: Sun Oct 11 16:07:41 2020 • BUILD SYSTEM: JelliaJamb