{"id":4699,"date":"2021-04-17T00:01:57","date_gmt":"2021-04-17T07:01:57","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=4699"},"modified":"2021-04-09T14:54:23","modified_gmt":"2021-04-09T21:54:23","slug":"zero-and-null-and-pointers-and-stuff","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=4699","title":{"rendered":"Zero and NULL and Pointers and Stuff"},"content":{"rendered":"<p>The defined constant <code>NULL<\/code> represents a null or empty pointer. I&#8217;ve <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=177\">written before<\/a> that the constant isn&#8217;t defined as zero, though such an assumption could lead you into trouble.<br \/>\n<!--more--><br \/>\nKeep in mind that <code>NULL<\/code> is a defined constant. It&#8217;s declared in the <code>stdio.h<\/code> header file or, more likely, its definition is made in another header file that&#8217;s referenced from <code>stdio.h<\/code>. Either way, the problem is that various C compilers can define <code>NULL<\/code> differently.<\/p>\n<p>Before diving into the details, be aware that <code>NULL<\/code> is a pointer constant. It&#8217;s not the same as integer value zero (though it might be defined that way). <code>NULL<\/code> is used where a pointer, a memory address, is to be evaluated or set as an argument.<\/p>\n<p>Also, <code>NULL<\/code> isn&#8217;t the same thing as the null character, <code>\\0<\/code>. This character is ASCII code zero, but it&#8217;s not a pointer. It&#8217;s a character value. (In some of my earlier books, I referred to <code>\\0<\/code> as the &#8220;NULL character,&#8221; which is incorrect.)<\/p>\n<p>Finally, <code>NULL<\/code> isn&#8217;t memory location (address) zero. It may be the value zero as it&#8217;s defined, but it&#8217;s not a memory location. Instead, think of it as an &#8220;empty&#8221; location and not zero; <code>NULL<\/code> indicates the absence of a memory location, which is important when comparing pointers: <code>NULL<\/code> evaluates as FALSE.<\/p>\n<p>The point [sic] of this post is that despite all this <code>NULL<\/code> nonsense, it&#8217;s unwise to assume <code>NULL<\/code>&#8216;s definition. From an email I recently received:<\/p>\n<blockquote><p>The problem with NULL is that it&#8217;s either defined as an integer zero constant or as an integer zero constant casted to a void pointer&#8230;<\/p><\/blockquote>\n<p>The assumption that <code>NULL<\/code> is always a pointer causes problems with some functions. The takeaway here is that it&#8217;s always a good idea to typecast <code>NULL<\/code> when you can.<\/p>\n<p>For example, if a function requires a <code>NULL<\/code> pointer as an argument, specify it like this:<\/p>\n<p><code>function(data, (char *)NULL);<\/code><\/p>\n<p>For fictitious <em>function()<\/em> above, the second argument is a <em>char<\/em> pointer. Yet, many compilers can swallow <code>NULL<\/code> by itself as the argument, so this statement might be okay:<\/p>\n<p><code>function(data, NULL);<\/code><\/p>\n<p>But to be safe consider typecasting <code>NULL<\/code> to the proper pointer data type, as in:<\/p>\n<p><code>(char *)NULL<\/code>.<\/p>\n<p>When the data type is unknown, use the <em>void<\/em> cast:<\/p>\n<p><code>(void *)NULL<\/code><\/p>\n<p>You may never experience an issue with <code>NULL<\/code> on your compiler. It may be liberal enough that it understands the difference between a pointer and the integer constant zero. Still, safe coding practice is always good. Since learning about this difference, I&#8217;ve double-checked my code to ensure that <code>NULL<\/code>s are typecast wherever possible.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The defined constant <code>NULL<\/code> isn&#8217;t zero, but is it always a pointer? <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=4699\">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-4699","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\/4699","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=4699"}],"version-history":[{"count":6,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4699\/revisions"}],"predecessor-version":[{"id":4718,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4699\/revisions\/4718"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4699"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4699"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4699"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}