{"id":6366,"date":"2024-04-27T00:01:29","date_gmt":"2024-04-27T07:01:29","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=6366"},"modified":"2024-04-20T10:57:45","modified_gmt":"2024-04-20T17:57:45","slug":"from-void-to-integer","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=6366","title":{"rendered":"From Void to Integer"},"content":{"rendered":"<p>You can&#8217;t just slap a data type upon a variable declared as <em>void<\/em>. No, a <em>void<\/em> type can be assigned a pointer. But even then, you can&#8217;t just typecast that pointer as an <em>int<\/em> and get away with it.<br \/>\n<!--more--><br \/>\nFrom <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=6360\">last week&#8217;s Lesson<\/a>, I devised the code below to properly allocate <em>void<\/em> variable <code>a<\/code> as storage for an integer. But the code generates a few mismatch warnings and crashes when the program runs:<\/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>Starting with the <em>scanf()<\/em> function, the <code>(int)<\/code> typecast attempts to convert a pointer into an integer, which doesn&#8217;t work. Pointers are memory locations &mdash; an address. You must fetch the <em>value<\/em> at the address. The nerds say &#8220;to dereference&#8221; the address, which requires the asterisk pointer operator. But where to put it? Here are some options, which I&#8217;m sure every C programmer new to pointers would attempt:<\/p>\n<p>A: <code>(int)*a<\/code><br \/>\nB: <code>*(int)a<\/code><br \/>\nC: <code>(int *)a<\/code><\/p>\n<p>You can rule out using the <code>&amp;<\/code> address-of operator. The <em>scanf()<\/em> function requires an address as its second argument in this instance. Variable <code>a<\/code> is already an address. So which of the above typecasts work?<\/p>\n<p><strong>A<\/strong> doesn&#8217;t work because variable <code>a<\/code> is an address of a <em>void<\/em> type. You don&#8217;t want the <em>value<\/em> at the address, you want the address itself. <strong>B<\/strong> doesn&#8217;t work because <code>a<\/code> is a pointer, which you can&#8217;t cast as an integer. The answer is <strong>C<\/strong> because it typecasts the address as the <em>location<\/em> of an integer value, which is what the <code>%d<\/code> placeholder desires.<\/p>\n<p>To fix the <em>printf()<\/em> statement takes a bit more brainwork. You need an integer value at an address, so the format <code>(int)a<\/code> (in the above code) doesn&#8217;t do anything. Rather than throw every combination of the characters <code>(<\/code>, <code>)<\/code>, <code>*<\/code>, and the word <code>int<\/code> at the wall to see what sticks, here is the proper format:<\/p>\n<p><code>*((int *)a)<\/code><\/p>\n<p>The <code>(int *)<\/code> cast is required as it was for the <em>scanf()<\/em> function to reference pointer <code>a<\/code> as containing an integer value. This expression, <code>(int *)a<\/code>, is then enclosed in parentheses and prefixed with the asterisk operator to fetch the integer value at the address stored at <code>a<\/code>. It&#8217;s ugly, but it works.<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2024_04_27-Lesson-a.c\" rel=\"noopener\" target=\"_blank\">2024_04_27-Lesson.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 final code runs like this:<\/p>\n<p><code>Enter an integer: 100<br \/>\nYou entered 100<\/code><\/p>\n<p>These steps are convoluted and completely unnecessary. In practice, if I wanted to mess with <em>void<\/em> variable <code>a<\/code> as an integer, I would just declare a proper pointer type, <code>int *b<\/code>, and assign the <em>void<\/em> pointer to it:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2024_04_27-Lesson-b.c\" rel=\"noopener\" target=\"_blank\">2024_04_27-Lesson-b.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    int *b;\r\n\r\n    a = malloc(sizeof(int));\r\n    b = a;\r\n\r\n    printf(\"Enter an integer: \");\r\n    scanf(\"%d\",b);\r\n    printf(\"You entered %d\\n\",*b);\r\n\r\n    return 0;\r\n}<\/pre>\n<p>This solution avoids the overhead (shown earlier) of referencing <em>void<\/em> variable <code>a<\/code> as an integer. Pointer <code>b<\/code> is a straight-up <em>int<\/em> pointer. It&#8217;s assigned the address stored in <em>void<\/em> pointer <code>a<\/code>, which handles the typecasting ordeal and renders unnecessary the extra decorations.<\/p>\n<p>Finally, remember that just declaring <code>a<\/code> as an <em>int<\/em> in the first place, would have avoided all this nonsense altogether. But then I&#8217;d have less to write about.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Yes, it&#8217;s possible to pervert a <em>void<\/em> type to an integer, but not easy or even necessary. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=6366\">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-6366","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\/6366","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=6366"}],"version-history":[{"count":6,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/6366\/revisions"}],"predecessor-version":[{"id":6393,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/6366\/revisions\/6393"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=6366"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=6366"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=6366"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}