{"id":2834,"date":"2017-12-09T00:01:13","date_gmt":"2017-12-09T08:01:13","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=2834"},"modified":"2017-12-02T12:24:27","modified_gmt":"2017-12-02T20:24:27","slug":"safe-coding-practices-terminating-a-string","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=2834","title":{"rendered":"Safe Coding Practices &#8211; Terminating a String"},"content":{"rendered":"<p>String constants and strings created or manipulated by C library functions all set that terminating null character, <code>'\\0'<\/code>. When you build your own strings, however, it&#8217;s easy to completely forget that null character. I know. I&#8217;ve done it.<br \/>\n<!--more--><br \/>\nA lot of safe coding practice in C deals with strings. From my books and courses, you&#8217;ve read that a string isn&#8217;t really a variable type in C; a string is a <em>char<\/em> array. The final character in the &#8220;string&#8221; array must be a null character, <code>\\0<\/code>. That character&#8217;s appearance is how the C language (and just about every other programming language) spies the end of a string &mdash; unless you forget to add that character.<\/p>\n<p>The following code builds a string and forgets to cap it with a null character:<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n#include &lt;stdlib.h&gt;\r\n\r\nint main()\r\n{\r\n    char *alphabet;\r\n    int a;\r\n\r\n    alphabet = (char *)malloc( sizeof(char)*26);\r\n    if( alphabet == NULL)\r\n    {\r\n        fprintf(stderr,\"Memory allocation error.\\n\");\r\n        exit(1);\r\n    }\r\n\r\n    for(a=0;a&lt;26;a++)\r\n        *(alphabet+a) = 'A'+a;\r\n\r\n    printf(\"Here's your alphabet: %s\\n\",alphabet);\r\n\r\n    return(0);\r\n}<\/pre>\n<p>Storage space for the string is allocated by the <em>malloc()<\/em> function at Line 9. Only a 26-character buffer is created, one for each letter of the Latin alphabet. (No room for the null character!) And the array is built in Lines 16 and 17. Line 19 prints the result, which looks like this on my computer:<\/p>\n<p><code>Here's your alphabet: ABCDEFGHIJKLMNOPQRSTUVWXYZ[?<\/code><\/p>\n<p>Output on another system might look different. For example, you might be lucky and memory is populated with zero-value bytes so the output looks perfect, but the code is still wrong: The string lacks a terminating character and storage lacks room for that character. The array is still valid, and it could be used as an array of single <em>char<\/em> values, just not as a string.<\/p>\n<p>To ensure that you&#8217;re working with a string and not just a collection of <em>char<\/em> variables waiting at a bus stop, cap the string with a null character, <code>\\0<\/code>. This practice must be done any time you cobble together a string, which happens frequently in C.<\/p>\n<p>Here is the improved code:<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n#include &lt;stdlib.h&gt;\r\n\r\nint main()\r\n{\r\n    char *alphabet;\r\n    int a;\r\n\r\n    alphabet = (char *)malloc( sizeof(char)*26+1);\r\n    if( alphabet == NULL)\r\n    {\r\n        fprintf(stderr,\"Memory allocation error.\\n\");\r\n        exit(1);\r\n    }\r\n\r\n    for(a=0;a&lt;26;a++)\r\n        *(alphabet+a) = 'A'+a;\r\n    *(alphabet+a) = '\\0';\r\n\r\n    printf(\"Here's your alphabet: %s\\n\",alphabet);\r\n\r\n    return(0);\r\n}<\/pre>\n<p>In Line 9, I added a <code>+1<\/code> inside the <em>malloc()<\/em> function. This format is a mnemonic to ensure that the null character is accounted for, and I (try to) do it in all my code. In fact, <a href=\"http:\/\/c-for-dummies.com\/blog\/?p=2801\">last week&#8217;s Lesson<\/a> used the <code>+1<\/code> in a <em>realloc()<\/em> statement that combines both strings.<\/p>\n<blockquote><p>Room for the null character must also be accounted for when you allocate a string array directly, as in <code>alphabet[27]<\/code>.<\/p><\/blockquote>\n<p>At Line 18, the null character is appended to the end of the <em>alphabet<\/em> array. The <code>alphabet+a<\/code> math works because I know that the value of <code>a<\/code> was incremented one final time after the <em>for<\/em> loop stopped spinning; the loop&#8217;s terminating condition is <code>a&lt;26<\/code>. Therefore, <code>a<\/code> must equal 26 after the <em>for<\/em> loop is done. At that position &mdash; <code>alphabet+a<\/code> or the 27th element of the array &mdash; the character <code>\\0<\/code> is set, terminating the string.<\/p>\n<p>The code is now safe and the string will be handled properly elsewhere in the code because its null-terminated. The output is also consistent:<\/p>\n<p><code>Here's your alphabet: ABCDEFGHIJKLMNOPQRSTUVWXYZ<\/code><\/p>\n","protected":false},"excerpt":{"rendered":"<p>All strings in C end with the null character &mdash; unless you forget to add one. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=2834\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-2834","post","type-post","status-publish","format-standard","hentry","category-main"],"_links":{"self":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2834","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2834"}],"version-history":[{"count":9,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2834\/revisions"}],"predecessor-version":[{"id":2866,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2834\/revisions\/2866"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2834"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2834"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2834"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}