Hi all,
I have a rather basic C/C++ question about the priority of the ++ operator in regard to pointers. Note the following code (joining two strings):

#define LENGTH 255
void mystrcat (char *base, char *add) {
    while(*base) ++base;
    while( *(base++) = *(add++) );
}
int main() {
    char name[LENGTH]="Feline";
    char lastname[LENGTH]=" Hazard";
    mystrcat(name,lastname);
    printf("My name is: %s", name);
return(0);
}

Now the function mystrcat joins lastname to name but I don't understand exactly why. At the end of the 1st while loop (line 3) base should be pointing to the terminal zero (\0) of the base string, right?
Now, the second while loop (line 4) I don't understand. What happens first? The assignment (*base)=(*add) and the increment of pointer positions? Or the other way around? First increment of position, and then assignment. I don't quite understand the prioritizing of actions in the 4th line.

Thanks,
-FH

Recommended Answers

All 7 Replies

When in doubt, break it into several lines. Think about it. If it works, the increment has to happen after the assignment and the while test has to happen AFTER the assignment. Otherwise you'd either get complete garbage or miss the first or last character or miss the entire last word. Rewrite it the way it needs to be, then reverse-engineeer it back to the code. It ends up being something like this.

    while(1)
    {
        *base = *add;
        add++; // same as ++add here
        if(*base == 0)
        {
            break;
        }
        base++; // same as ++base here
    }

I'm not 100% sure that the add++ line is in the correct spot, but as you'll see for the code, it can be anywhere as long as it's not before the

*base = *add

line.

commented: Thank you, sir, for a sane and rational answer. +4

Now, the second while loop (line 4) I don't understand. What happens first? The assignment (base)=(add) and the increment of pointer positions? Or the other way around? First increment of position, and then assignment.

Think about how a while loop works. In general what's done first -- the comparison or the inside of the loop?
In other words, given while (A) B;
Is A first, or is B first?

@VernonDozier I agree that the assignment happens first, or else bad things would happen and the function would not do its job. The problem is, that I don't understand how I was supposed to know that before trying out the function. If I had been given a slightly different example, I'm not sure I'd know how to read it.
while( *(base++) = *(add++) );
The fact is, that parentesses usually imply priority, in every construct I know. So reading it, I first thought "Hmmm there are parentesses, so probably the increment of the pointer happens first, and therefore the program has a bug". When I tested it, it was obvious that the increment happens only after the assignment, and the function is correct.

So, if I were to translate C++'s *, ++,= and () expressions to 'math', my question would be what's the priority of implementing these instructions? Like that in normal math division and multiply comes before + and - for example.
Its just that not always I'd be able to try things out to know if they are correct. (Like in a test cough cough ;-) )

@WaltP : The expression first evaluated is of course the condition of the while statement, (A). This example is not similar to my line #3, in which both the 'test' and the assignment are in the same expression. I do not quite understand how your question is relevant. Can you elaborate?

Thanks,
-FH

#include <iostream>
using namespace std;


int main()
{
    int a = 0;
    while(a < 20)
    {
        cout << "Top of loop\n";
        cout << a << endl;
        cout << a++ << endl;
        cout << a << endl;
        cout << ++a << endl;
        cout << a << endl;
        int b = a++;
        cout << a << " " << b << endl;
        b = ++a;
        cout << a << " " << b << endl;
    }

    return 0;
}

b = ++a;

means

a = a + 1;
b = a;

b = a++;

means

b = a;
a = a + 1;

So if the ++ comes before the variable, increment first, then assign. If after the variable, do the opposite.

Note: Parentheses always supercede everything else.

Oh.
That was actually quite clear and simple. Thanks! :-)
-FH

Using increment/decrement operators in function arguments is a common beginner mistake. According to the C and C++ standards, the order of evaluation of the arguments passed to a function is completely up to the compiler writers. So, in the function void func(a++, b--) you don't know if a++ will be executed before b--, though they will both be complete before the function is entered. An example of this sort of problem caused serious issues with the manufacturing execution system my previous company wrote/sold (it runs most of the 300mm semiconductor fabs in the world), and finding the root cause took me several days of intense work in a debugger. I only got called in to deal with this stuff after the responsible engineers for that module had spent the better part of a month trying to diagnose the problem...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.