{"id":177,"date":"2013-07-27T00:01:57","date_gmt":"2013-07-27T08:01:57","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=177"},"modified":"2020-06-24T20:22:26","modified_gmt":"2020-06-25T03:22:26","slug":"the-difference-between-null-and-zero","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=177","title":{"rendered":"The Difference Between NULL and Zero"},"content":{"rendered":"<p>University professors will try to drive the point into your head all semester: <code>NULL<\/code> and zero are not the same thing! But yet, you&#8217;ve looked into memory, you&#8217;ve examined the code. That thing they call <code>NULL<\/code> sure does look like a zero! What&#8217;s the big difference?<br \/>\n<!--more--><br \/>\nStraight up, <code>NULL<\/code> is a pointer. It&#8217;s a constant, made available through <code>stdio.h<\/code>.<\/p>\n<p>Whenever you see the <code>NULL<\/code> constant used, it&#8217;s as a pointer value. It can also be used to typecast pointers, such as <code>(int *)NULL<\/code>. And keep in mind the pointer mantra:<\/p>\n<p><em>A pointer is a variable that holds a memory location.<\/em><\/p>\n<p>Zero is a value. It&#8217;s not a memory location, as only pointers can hold memory locations. Further, pointers hold memory locations that reference something. It&#8217;s unwise (and often prohibited) to stuff a pointer with an immediate value just because you want to peek somewhere into memory.<\/p>\n<p>Perhaps the element that causes the most confusion is the <code>\\0<\/code> escape character. That&#8217;s the <em>null character<\/em>, which is used in C to terminate a string of text. The problem is that <code>\\0<\/code> translates into character value zero. In fact, you can use a zero directly (if you know how) and it has the same effect. But that doesn&#8217;t mean that zero and <code>NULL<\/code> are the same thing, especially given that <code>NULL<\/code> is one use of the word and &#8220;null character&#8221; is another.<\/p>\n<p>The following code should confuse you further, if you aren&#8217;t confused enough already:<\/p>\n<pre class=\"screen\">#include &lt;stdio.h&gt;\r\n\r\nint main()\r\n{\r\n    printf(\"NULL is %p\\n\",(int *)NULL);\r\n    printf(\"\\\\0 is %d\\n\",'\\0');\r\n    printf(\"Size of int = %ld\\n\",sizeof(int));\r\n    printf(\"Size of NULL = %ld\\n\",sizeof(NULL));\r\n\r\n    return(0);\r\n}<\/pre>\n<p>This code attempts to display the value of <code>NULL<\/code>. As an address, the first line output gives its address:<\/p>\n<p><code>NULL is 0x0<\/code><\/p>\n<p>The <code>NULL<\/code> pointer holds address zero, which may cause you to flare your nostrils, point at the screen, and shout, &#8220;I told you!&#8221; But it&#8217;s still not a zero in the &#8220;Isn&#8217;t <code>NULL<\/code> equal to zero?&#8221; sense.<\/p>\n<p>The second line demonstrates how the <code>\\0<\/code> escape character is really value zero:<\/p>\n<p><code>\\0 is 0<\/code><\/p>\n<p>Keep in mind that no one is offended when you use a zero value to terminate a string; the <code>\\0<\/code> escape sequence simply allows you to specify it within a set of quotes. That may be required, for example, when a function demands a <em>char<\/em> value as input and not an <em>int<\/em>.<\/p>\n<p>Finally, to prove each item is of the appropriate type, the code outputs these lines:<\/p>\n<p><code>Size of int = 4<br \/>\nSize of NULL = 8<\/code><\/p>\n<p>The <em>int<\/em> value <code>\\0<\/code> is 4 bytes long (on my computer), which helps confirm it&#8217;s an <em>int<\/em>. But the <code>NULL<\/code> value is 8 bytes long, which is the size of an address.<\/p>\n<p>Even when all this still confuses you, just drive it into your head: <code>NULL<\/code> isn&#8217;t used as a placeholder for zero. In the C language, which features a memory location variable type (the pointer), it&#8217;s necessary to have <code>NULL<\/code> to represent an empty memory location. That&#8217;s the purpose of the <code>NULL<\/code> constant.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I frequently get asked the question whether or not <code>NULL<\/code> is zero. It isn&#8217;t, but the explanation why is a bit more involved. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=177\">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-177","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\/177","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=177"}],"version-history":[{"count":8,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/177\/revisions"}],"predecessor-version":[{"id":4230,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/177\/revisions\/4230"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=177"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=177"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=177"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}