What the Heck is That Thing?

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.

Here is the error I witnessed when I compiled one of my handy utility programs:

warning: using the result of an assignment as a condition without parentheses [-Wparentheses]
                                                while((*a++) = (*c++))
                                                      ~~~~~~~^~~~~~~~

The program runs, of course. I’ve been using it for years. And the warning message is easily addressed by adding another set of parentheses:

while( ((*a++) = (*b++)) )
    ;

Still, this construction is weird. What the heck does it do? What was I trying to do when I coded the thing. Honestly, I have no idea.

Further, this expression (or a similar one) appears seven times in the code. It’s not a mistake; I don’t mean to compare the values in the two pointers. No, it’s an assignment. The loop keeps repeating until the value assigned is the null character or zero. My guess is that I’m building a string, but the problem is that the code utterly lacks comments.

Yes, it’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: a and b?. For me, as someone who teaches C programming and should know better, this situation is very frustrating.

To seek to better understand what the expression accomplishes, I decided to write a separate program to put the while loop to work. Here is what I came up with:

2023_04_29-Lesson.c

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main()
{
    char *sto,*a;
    const char *b = "string\n";

    sto = malloc( sizeof(char)*strlen(b) + 1 );

    a = sto;
    while( ((*a++) = (*b++)) )
        ;

    printf("%s",sto);

    return(0);
}

Storage is allocated for pointer sto, enough to store the string referenced by constant b. Pointer a is a reference. Once it’s initialized to the address of sto, the while loop copies characters from b to a. The loop repeats until the null character is copied, which makes the expression FALSE. The string is copied and the loop stops.

Here’s the output:

string

This small program demonstrated what I thought was going on with the weirdo while loop: It duplicates a string. More specifically, the code builds a string, adding characters to a buffer a from other sources referenced by pointer b. 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 “brain dump.”

Fast forward ten years to the time I must re-compile the code and — ta-da! — confusion reigns. Yet another good lesson in why it’s important to comment your code, even the silly stuff you write for yourself.

Leave a Reply