{"id":6360,"date":"2024-04-20T00:01:22","date_gmt":"2024-04-20T07:01:22","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=6360"},"modified":"2024-04-27T09:20:38","modified_gmt":"2024-04-27T16:20:38","slug":"out-of-the-void","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=6360","title":{"rendered":"Out of the <em>void<\/em>"},"content":{"rendered":"<p>The four standard data types in C programming are <em>char<\/em>, <em>int<\/em>, <em>float<\/em>, and <em>double<\/em>. A fifth type is <em>void<\/em>, which is used to declare storage of an unknown type. My goal is to see how badly I can abuse this data type.<br \/>\n<!--more--><br \/>\nThe <em>void<\/em> data type used primarily with pointers. For example, in <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=1287\">the <em>qsort()<\/em> function<\/a>, the <em>compar()<\/em> function is prototyped like this:<\/p>\n<p><code>int (*compar)(const void *, const void *));<\/code><\/p>\n<p>The <code>const void *<\/code> argument represents a pointer of an unknown type. When you code your own <em>compar()<\/em> function, you substitute <code>const void *<\/code> with a pointer based on the data type of whatever data the <em>qsort()<\/em> function is sorting. The <em>void<\/em> placeholder serves to represent a non-specific pointer type, but can I pervert it to be used for a standard data type on-the-fly?<\/p>\n<p>In the following code, I need an integer value. I declare variable <code>a<\/code> as a <em>void<\/em>, which is wrong but will it work?<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2024_04_20-Lesson-a.c\" rel=\"noopener\" target=\"_blank\">2024_04_20-Lesson-a.c<\/a><\/h3>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nint main()\r\n{\r\n    void a;\r\n\r\n    printf(\"Enter an integer: \");\r\n    scanf(\"%d\",a);\r\n    printf(\"You entered %d\\n\",a);\r\n\r\n    return 0;\r\n}<\/pre>\n<p>This code won&#8217;t build. The error generated is &#8220;incomplete type.&#8221; The <em>void<\/em> type doesn&#8217;t allocate storage for anything (hence, &#8220;void&#8221;). The C language documentation says that <em>void<\/em> can be cast. So how about this modification:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2024_04_20-Lesson-b.c\" rel=\"noopener\" target=\"_blank\">2024_04_20-Lesson-b.c<\/a><\/h3>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nint main()\r\n{\r\n    void a;\r\n\r\n    printf(\"Enter an integer: \");\r\n    scanf(\"%d\",(int)a);\r\n    printf(\"You entered %d\\n\",(int)a);\r\n\r\n    return 0;\r\n}<\/pre>\n<p>Above, I&#8217;ve added the <code>(int)<\/code> typecast to variable <code>a<\/code> where it&#8217;s used in the <em>scanf()<\/em> and <em>printf()<\/em> functions. The code doesn&#8217;t build, again generating an &#8220;incomplete type&#8221; error. So much for being sneaky.<\/p>\n<p>The <em>void<\/em> data type cannot be used to represent a value in any way, specifically to convert a non-<em>void<\/em> type, which is what I&#8217;m trying to do in this code.<\/p>\n<p>Obviously, the solution is to declare variable <code>a<\/code> as an <em>int<\/em>. With this modification, the program runs and everyone gets a cupcake. But I&#8217;m stubborn about these things.<\/p>\n<p>The <em>void<\/em> type is used with pointers, as shown earlier with the <em>compar()<\/em> prototype example. So I can use the <em>void<\/em> declaration to assign a storage for an integer, then use its pointer to manipulate data. Here&#8217;s a final modification:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2024_04_20-Lesson-c.c\" rel=\"noopener\" target=\"_blank\">2024_04_20-Lesson-c.c<\/a><\/h3>\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    void *a;\r\n\r\n    a = malloc(sizeof(int));\r\n\r\n    printf(\"Enter an integer: \");\r\n    scanf(\"%d\",(int)a);\r\n    printf(\"You entered %d\\n\",(int)a);\r\n\r\n    return 0;\r\n}<\/pre>\n<p>The <em>malloc()<\/em> function allocates an integer-sized chunk of storage for variable <code>a<\/code>. This program builds, but generates three warnings regarding the <code>(int)<\/code> cast. Here&#8217;s the output:<\/p>\n<p><code>Enter an integer: 100<br \/>\nSegmentation fault<\/code><\/p>\n<p>At this point, the code approaches one of those challenges where beginning C programmers know that an asterisk is required &mdash; but where? Two puzzles must be solved.<\/p>\n<p>The first puzzle is that the <em>scanf()<\/em> function requires the address of a value &mdash; a pointer. Variable <code>a<\/code> is a pointer. But do you typecast it? What about this: <code>scanf(\"%d\",a);<\/code> ? If you try, which kind of makes sense, you get a type mismatch error as <code>%d<\/code> requires an integer and <code>a<\/code> is a <em>void<\/em> pointer type.<\/p>\n<p>The second puzzle occurs in the <em>printf()<\/em> statement, where <code>a<\/code> is again a <em>void<\/em> pointer despite being cast an an integer. How do you properly code this value as an integer?<\/p>\n<p>In <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=6366\">next week&#8217;s Lesson<\/a> I cover how to resolve these issues.<\/p>\n","protected":false},"excerpt":{"rendered":"<p><em>void<\/em> is a data type in the C language, but a special data type. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=6360\">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-6360","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\/6360","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=6360"}],"version-history":[{"count":5,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/6360\/revisions"}],"predecessor-version":[{"id":6400,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/6360\/revisions\/6400"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=6360"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=6360"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=6360"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}