{"id":5850,"date":"2023-04-29T00:01:34","date_gmt":"2023-04-29T07:01:34","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=5850"},"modified":"2023-04-22T10:01:41","modified_gmt":"2023-04-22T17:01:41","slug":"what-the-heck-is-that-thing","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=5850","title":{"rendered":"What the Heck is That Thing?"},"content":{"rendered":"<p>I just moved to a new computer. The process involved taking all my personal programs and re-compiling them for the new system. It took a while, but fortunately everything works with my various programs doing their necessary jobs. That is, unless I see a weirdo error upon compiling one of those golden oldies. On such error I saw made me wonder what they heck I was doing in my code.<br \/>\n<!--more--><br \/>\nHere is the error I witnessed when I compiled one of my handy utility programs:<\/p>\n<p><code>warning:&nbsp;using&nbsp;the&nbsp;result&nbsp;of&nbsp;an&nbsp;assignment&nbsp;as&nbsp;a&nbsp;condition&nbsp;without&nbsp;parentheses&nbsp;[-Wparentheses]<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;while((*a++)&nbsp;=&nbsp;(*c++))<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;~~~~~~~^~~~~~~~<\/code><\/p>\n<p>The program runs, of course. I&#8217;ve been using it for years. And the warning message is easily addressed by adding another set of parentheses:<\/p>\n<p><code>while( ((*a++) = (*b++)) )<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;;<\/code><\/p>\n<p>Still, this construction is weird. What the heck does it do? What was <em>I<\/em> trying to do when I coded the thing. Honestly, I have no idea.<\/p>\n<p>Further, this expression (or a similar one) appears seven times in the code. It&#8217;s not a mistake; I don&#8217;t mean to compare the values in the two pointers. No, it&#8217;s an assignment. The loop keeps repeating until the value assigned is the null character or zero. My guess is that I&#8217;m building a string, but the problem is that the code utterly lacks comments.<\/p>\n<p>Yes, it&#8217;s typical of me to quickly cobbled together a utility and not bother to comment anything. Of the 300-plus lines of code in the file, nary a comment is to be found. I know what the program does, but even the variables are poorly named: <code>a<\/code> and <code>b<\/code>?. For me, as someone who teaches C programming and should know better, this situation is very frustrating.<\/p>\n<p>To seek to better understand what the expression accomplishes, I decided to write a separate program to put the <em>while<\/em> loop to work. Here is what I came up with:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2023_04_29-Lesson.c\" rel=\"noopener\" target=\"_blank\">2023_04_29-Lesson.c<\/a><\/h3>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n#include &lt;stdlib.h&gt;\r\n#include &lt;string.h&gt;\r\n\r\nint main()\r\n{\r\n    char *sto,*a;\r\n    const char *b = \"string\\n\";\r\n\r\n    sto = malloc( sizeof(char)*strlen(b) + 1 );\r\n\r\n    a = sto;\r\n    while( ((*a++) = (*b++)) )\r\n        ;\r\n\r\n    printf(\"%s\",sto);\r\n\r\n    return(0);\r\n}<\/pre>\n<p>Storage is allocated for pointer <code>sto<\/code>, enough to store the string referenced by constant <code>b<\/code>. Pointer <code>a<\/code> is a reference. Once it&#8217;s initialized to the address of <code>sto<\/code>, the <em>while<\/em> loop copies characters from <code>b<\/code> to <code>a<\/code>. The loop repeats until the null character is copied, which makes the expression FALSE. The string is copied and the loop stops.<\/p>\n<p>Here&#8217;s the output:<\/p>\n<p><code>string<\/code><\/p>\n<p>This small program demonstrated what I thought was going on with the weirdo <em>while<\/em> loop: It duplicates a string. More specifically, the code builds a string, adding characters to a buffer <code>a<\/code> from other sources referenced by pointer <code>b<\/code>. It would have been so much easier to just jot that information down in a comment. But no. I was probably in a hurry doing what I refer to as a &#8220;brain dump.&#8221;<\/p>\n<p>Fast forward ten years to the time I must re-compile the code and &mdash; <em>ta-da!<\/em> &mdash; confusion reigns. Yet another good lesson in why it&#8217;s important to comment your code, even the silly stuff you write for yourself.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A good lesson in why you should comment your code, even the stuff you write for yourself. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=5850\">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-5850","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\/5850","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=5850"}],"version-history":[{"count":4,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/5850\/revisions"}],"predecessor-version":[{"id":5861,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/5850\/revisions\/5861"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5850"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5850"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5850"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}