Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C/C++ Post-increment by more than one

I'm reading bytes from a buffer. But sometimes what I'm reading is a word or longer.

// assume buffer is of type unsigned char *
read_ptr(buffer+(position++))

That's fine but how can I post-increment position by 2 or 4? There's no way I can get the += operator to post-increment, is there?

Reason is, I have this big awful expression which I want to evaluate, while at the same time incrementing the position variable.

I think I came up with my own solution. I'm pretty sure it works. Everyone's gonna hate it though, since this isn't very readable code.

read_ptr(buffer+(position+=4)-4)

I will then make this into a macro after testing it a bit to make sure it's doing the right thing.

IN CONCLUSION:

Don't do this. It's just a bad idea because this is the sort of thing that generates unmaintainable code. But... it does turn out to be quite easy to convert any pre-incrementing operator into a post-incrementing one.

like image 330
Steven Lu Avatar asked Mar 19 '11 06:03

Steven Lu


People also ask

What is ++ i and i ++ in C?

In C, ++ and -- operators are called increment and decrement operators. They are unary operators needing only one operand. Hence ++ as well as -- operator can appear before or after the operand with same effect. That means both i++ and ++i will be equivalent.

Is i ++ the same as i i 1?

These two are exactly the same. It's just two different ways of writing the same thing. i++ is just a shortcut for i += 1 , which itself is a shortcut for i = i + 1 .

What is the difference between Preincrement and Postincrement in C?

Preincrement and Postincrement in C are the two ways to use the increment operator. In Pre-Increment, the operator sign (++) comes before the variable. It increments the value of a variable before assigning it to another variable. In Post-Increment, the operator sign (++) comes after the variable.

What does += mean in C++?

+= Add AND assignment operator, It adds right operand to the left operand and assign the result to left operand. C += A is equivalent to C = C + A.


2 Answers

how can I post-increment position by 2 or 4?

You can't post-increment a variable by 2 or 4 but you can use the following (in your case)

read_ptr(buffer+position); position += 2;

like image 121
Prasoon Saurav Avatar answered Sep 27 '22 21:09

Prasoon Saurav


Although, I would not recommend this solution, but if you don't want to change this line in your code:

read_ptr(buffer+(position++));

And you still want to post-increment position by 2, then define position as Index position(2); where the type Index is defined here, and also shown the usage:

struct Index
{
    int step;
    int value;
    Index(int s=1, int v=0): step(s), value(v) {}
    Index operator++(int) 
    { 
       Index prev(step, value); 
       value += step; 
       return prev;
    }
    operator int() { return value; }
};

int main() {
        char arr[] = "1234567890" ;

        cout <<"Increment by 2" <<endl;
        Index i2(2); //increment by 2
        cout << *(arr + (i2++)) << endl;
        cout << *(arr + (i2++)) << endl;
        cout << *(arr + (i2++)) << endl;
        cout << *(arr + (i2++)) << endl;

        cout <<"Increment by 3" <<endl;        
        Index i3(3); //increment by 3
        cout << *(arr + (i3++)) << endl;
        cout << *(arr + (i3++)) << endl;
        cout << *(arr + (i3++)) << endl;
        cout << *(arr + (i3++)) << endl;
        return 0;
}

Output:

Increment by 2
1
3
5
7
Increment by 3
1
4
7
0

Working Example : http://ideone.com/CFgal

Note: I would still not suggest this solution in real life project. It's more like puzzle :D

like image 29
Nawaz Avatar answered Sep 27 '22 21:09

Nawaz