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

若如初见. 提交于 2019-12-22 05:26:24

问题


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.


回答1:


The += operator would be a separate statement (not post or pre increment). You could use the following line:

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



回答2:


You don't; you break it up into more than one line. There is no reason to stuff everything into one line here.

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



回答3:


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;




回答4:


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




回答5:


If position were a pointer to int16 or int32, incrementing it would add 2 or 4, respectively.




回答6:


In C++, you can easily write a function to perform a post-style double-increment:

template <typename T>
T inc2(T &t) {
    T r(t);
    ++t; // or t++ if you want to respect inconsistently-overloaded operators,
    ++t; // but I wouldn't bother.
    return r;
}

read_ptr(buffer+inc2(position))

In C it's slightly more awkward:

size_t inc2(size_t *s) { // or whatever type you're using
    size_t r = *s;
    (*s) += 2;
    return r;
}

read_ptr(buffer+inc2(&position))

You can cover the 4 case as well by making it an additional function parameter, or perhaps an additional template parameter in the C++ case.

There's a second question, whether it's worth pursuing this style of programming in C++ or in C, where you do so much in a single statement. Avoiding side-effects can make the code easier to understand, even though it comes out longer.




回答7:


Well, I did answer my question in the edit... Basically what I wanted was a single expression which evaluates to the original value but has a side effect of incrementing by an arbitrary amount. Here are some macros.

#define INC(x,inc) (((x)+=(inc))-(inc))
#define INC2(x) INC(x,2)
#define INC4(x) INC(x,4)
#define INC8(x) INC(x,8)


来源:https://stackoverflow.com/questions/5360627/c-c-post-increment-by-more-than-one

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!